C程序算法题二

1.将一张百元大钞对换成1元、5元、10元的小钞,要求每种小钞最少要一张,编程求共有多少种对换方法

#include "stdio.h"
void main()
{
    
    
	int a,b,c,n;
	for(a=1;a<100;a++){
    
    
		for(b=1;b<20;b++){
    
    
			for(c=1;c<10;c++){
    
    
				if(a+b*5+c*10==100){
    
    
					n++;
				}
			}
		}
	}
	printf("一共有%d种方法", n);
}

2.鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一,白钱买白鸡。问鸡翁、母、雏各几何?输出所有买法,每一种鸡至少要一只。

#include<stdio.h>

int main(){
    
    
	int roo,hen,chick;
	for(roo=1;roo<20;roo++){
    
    
		for(hen=1;hen<33;hen++){
    
    
			chick=100-roo-hen;
			if(roo*5+hen*3+chick/3==100){
    
    
				printf("roo:%d hen:%d chick:%d\n", roo, hen, chick);
			}
		}
	}
	return 0;
} 

3.利用指针统计一个字符串中,字母、空格、数字、及其它字符的个数

#include <stdio.h>

int main( )                      
{
    
     
    int alpha,digit,space,other;
    char s[80],*p;
    alpha=digit=space=other=0;
    printf("input string:\n");
    gets(s);
    for(p=s;*p!='\0';p++){
    
    
    	if((*p>='A' && *p<='Z')||(*p>='a' && *p<='z')){
    
    
    		alpha++;
		}else if(*p==' '||*p=='\t'){
    
    
			space++;
		}else if(*p>'0'&&*p<'9'){
    
    
			digit++;
		}else{
    
    
			other++;
		}
	}
	printf("alpha:%d digit:%d space:%d other:%d\n", alpha, digit, space, other);
	return 0;	
}

4.找出二维数组 a 中的最大元素,并要求输出该元素以及该元素的行号和列号,二维数组元 素可以从键盘输入也可以定义时直接赋初值

#include<stdio.h>

int main() 
{
    
    
	int a[3][4]={
    
    {
    
    1,2,334,3},{
    
    4,2,43,6654,},{
    
    98,9,59,599}};
	int i,j,imax,jmax;
	imax=jmax=0;
	for(i=0;i<3;i++){
    
    
		for(j=0;j<4;j++){
    
    
			if(a[i][j]>a[imax][jmax]){
    
    
				imax=i;
				jmax=j;
			}
		}
	}
	printf("The max number is:a[%d][%d]\n", imax, jmax);
	return 0;
} 

5.打印 2~500 间的全部素数,同时求其和。(只能被 1 和这个数自身整除的数叫素数)

#include<stdio.h>
#include<math.h>
int main() 
{
    
      	 
	int m,k,i,leap=1,count=0,sum=0;
	for(m=2;m<=500;m++){
    
    
		k=sqrt(m+1);
		for(i=2;i<=k;i++){
    
    
			if(m%i==0){
    
    
				leap=0;//不是素数
				break; 
			}
		}
		if(leap){
    
    
			sum+=m;
			printf("%-4d", m);
			count++;
			if(count%10==0){
    
    
				printf("\n");
			}
		}
		leap=1;
	}
	printf("\nThe count is:%d", count);
	printf("\nThe sum is:%d", sum);
	return 0;
}

6.编程,在[1,98]查找并输出所有满足条件“x+x+1+x+2的平方根是整数”的数x(如输出2、11,因为2+3+4的和为9,11+12+13和为36,他们的平方根3、6都是整数)

#include <stdio.h>
#include<math.h>
void main()
{
    
      
    int i,s;
	for(i=1;i<=98;i++) {
    
    
	    s=3*i+3;
		if(sqrt(s)==(int)sqrt(s)){
    
    
			printf("%d\n",i);
		}
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44337241/article/details/115027830