资源描述:
《c语言编程题》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
四、编程题(2道小题,共20分)1、求100之内自然数中最大的能被17整除的数。#includevoidmain(){inti;for(i=100;i>0;i--)if(i%17==0)break;printf("%d ",i);}2、已知a,b,c都是1位整数,求当三位整数abc、cba的和为1333时a、b、c的值。#includevoidmain(){inta,b,c;for(a=1;a<=9;a++){for(b=0;b<=9;b++){for(c=1;c<=9;c++)if(a*100+b*10+c+c*100+b*10+a==1333)printf("%d,%d,%d ",a,b,c);}}}3、计算并输出200-400之间不能被3整除的整数的和。#includevoidmain(){inti,sum=0;for(i=200;i<=400;i++)if(i%3!=0)sum=sum+i;printf("%d ",sum);}4、从键盘输入10个数,统计非负数的个数,并计算非负数的和。#includevoidmain(){inti,n=0,sum=0;inta[10]; printf(“请输入10个数:”);for(i=0;i<10;i++)scanf(“%d”,&a[i]);for(i=0;i<10;i++)if(a[i]>=0){sum=sum+a[i];n++;}printf("非负个数是%d,sum=%d ",n,sum);}5、求100之内自然数中偶数之和。#includevoidmain(){inti,sum=0;for(i=0;i<=100;i++)if(i%2==0)sum=sum+i;printf("sum=%d ",sum);}6、输入5个数,求和并输出。要求编写求和的函数。#includevoidmain(){intb(intx[],intn);intsum=0;inta[5];inti;printf("请输入5个数:");for(i=0;i<5;i++)scanf("%d",&a[i]);sum=b(a,5);printf("sum=%d ",sum);}intb(intx[],intn){inti,sum=0;for(i=0;i<5;i++)sum=sum+x[i];returnsum;}7、编程计算1*2*3+3*4*5+5*6*7+...+99*100*101的值。#includevoidmain(){inti,sum=0;for(i=1;i<=99;i=i+2) sum=sum+i*(i+1)*(i+2);printf("%d ",sum);}8、编写程序,将用户输入的字符串中所有的字符a用*代替,然后输出。#include#includevoidmain(){inti,n;charb[20];printf("请输入字符串:");gets(b);n=strlen(b);for(i=0;ivoidmain(){inti;inta[5];printf(“请输入5个数:”);for(i=0;i<5;i++)scanf(“%d”,&a[i]);for(i=0;i<2;i++){t=a[i];a[i]=a[4-i];a[4-i]=t;}for(i=0;i<5;i++)printf("%d",b[i]);printf(" ");}10、求3行3列矩阵a[3][3]={1,2,3,4,5,6,7,8,9}非对角线上元素之和。 #includevoidmain(){inti,j,sum=0;inta[3][3]={1,2,3,4,5,6,7,8,9};for(i=0;i<3;i++)for(j=0;j<3;j++)if(i!=j&&i+j!=2)sum=sum+a[i][j];printf("%d",sum);printf(" ");}11、编程判断输入的整数的正负性和奇偶性。如果为正数,输出z;如果为负数,输出f;如果为偶数,输出o;如果为奇数,输出j#includevoidmain(){inta;printf(“请输入一个数:”);scanf(“%d”,&a);if(a>0)printf("z ");if(a<0)printf("f ");if(a%2==0)printf("o ");if(a%2!=0)printf("j ");}12、计算并输出1-200之间不能被5整除的整数的和。#includevoidmain(){inti,sum=0;for(i=1;i<=200;i++)if(i%5!=0)sum=sum+i;printf("%d ",sum);}13、从键盘输入n和a的值,计算a+aa+aaa+...+aa...a(n个a)的值。#includevoidmain(){inti,a,n;longintsum=0,st=0;printf("请输a和n:");scanf("%d%d",&a,&n);for(i=1;i<=n;i++) {st=st+a;sum=sum+st;a=a*10;}printf("%ld ",sum);}14、输入5个数,求它们中最大值和平均值并输出。#includevoidmain(){inta[5];inti,j,max,sum=0;floataverage;printf("请输入5个数:");for(i=0;i<5;i++)scanf("%d",&a[i]);max=a[0];for(j=0;j<5;j++){sum=sum+a[j];if(maxvoidmain(){inti;for(i=200;i<=400;i++)if((i%3==0)&&((i-6)%10==0))printf("%d ",i);printf(" ");}16、编写程序,将用户输入的字符串中所有的字符a去掉,然后输出剩余的字符。#includevoidmain(){inti,j,n1;chara[20];printf("请输入字符串:");gets(a); n1=strlen(a);for(i=0;ivoidmain(){inti,sum=0;for(i=200;i<=400;i++)if(i%7!=0)sum=sum+i;printf("sum=%d ",sum);}18.计算并输出200-400之间不能被5整除的整数的和。#includevoidmain(){inti,sum=0;for(i=200;i<=400;i++)if(i%5!=0)sum=sum+i;printf("sum=%d ",sum);}19、从键盘输入10个数,统计非正数的个数,并计算非正数的和#includevoidmain(){inti,n=0,sum=0;inta[10];printf(“请输入10个数:”);for(i=0;i<10;i++)scanf(“%d”,&a[i]);for(i=0;i<10;i++)if(a[i]<=0){sum=sum+a[i];n++;}printf("非正个数是%d,sum=%d ",n,sum);}20、输入一串字符,将其中的大写字母变成对应的小写字母并输出。#include#include voidmain(){inti,n;chara[20];printf("请输入字符串:");gets(a);n=strlen(a);for(i=0;i='A'&&a[i]<='Z')a[i]=a[i]+32;for(i=0;ivoidmain(){inta,b,c,s;for(a=1;a<=9;a++){for(b=0;b<=9;b++){for(c=0;c<=9;c++)if(a*100+b*10+c==a*a*a+b*b*b*b+c*c*c){s=a*100+b*10+c;printf("%d ",s);}}}}22、一个皮球从100米高度自由落下,每次落地后反弹回原高度的一半,再落下,再反弹。求当它第10次落地时,共经过了多少米,第10次反弹多高?#includevoidmain(){inti;floatsum=100.0,h=50;for(i=2;i<=10;i++){sum=sum+2*h;h=h/2;} printf("%f,%f ",sum,h);}23、输出所有0-200之间能被3整除且个位数字为6的整数。#includevoidmain(){inti;for(i=0;i<=200;i++)if((i%3==0)&&((i-6)%10==0))printf("%d ",i);printf(" ");}24、输入一个正整数,输出它的阶乘。#includevoidmain(){inti,J=1,n=10;printf(“请输入一个正整数:”);scanf(“%d”,&n);for(i=1;i<=n;i++)J=J*i;printf("%d的阶乘是:%d ",n,J);}25、编写程序,判断从键盘输入的字符中数字字符的个数、大写字母的个数、小写字母的个数及其它字符的个数,以*作为字符输入结束标志。#include#includevoidmain(){inti,x,m=0,n=0,k=0,j=0;chara[20];printf("请输入字符串:");gets(a);x=strlen(a);for(i=0;i='0'&&a[i]<='9')m++;elseif(a[i]>='A'&&a[i]<='Z')n++;elseif(a[i]>='a'&&a[i]<='z')k++;else j++;}printf("%d,%d,%d,%d ",m,n,k,j);}26、编写程序,使用循环结构输出下列图形:********************#includevoidmain(){inti,j,k;for(i=0;i<5;i++){for(j=0;j<4-i;j++)printf("");for(k=0;k<4;k++)printf("*");printf(" ");}printf(" ");}27、输入一串字符,逆序输出。要求使用数组实现。#include#includevoidmain(){inti,j,t,n;chara[10];printf("请输入字符串:");gets(a);n=strlen(a);for(i=0;ivoidmain(){intn,s=1;floatsum=1.0;for(n=2;n<=100;n++){s=-s;sum=sum+1.0/n*s;}printf("1-1/2+1/3-1/4+...+1/99-1/100=%f ",sum);}29、输入3个学生4门课的成绩{(60,70,65,75),(75,80,75,90),(95,75,90,65)},计算每个学生的总分和平均成绩并输出,结果保留一位小数。#includevoidmain(){inta[3][4]={{60,70,65,75},{75,80,75,90},{95,75,90,65}};inti,j;floataverage,sum=0.0;printf("三个学生的总分和平均成绩依次为: ");for(i=0;i<3;i++){sum=0.0;for(j=0;j<4;j++)sum=sum+a[i][j];average=sum/4;printf("%.2f,%.2f ",sum,average);}}30、求两个正整数的最大公约数。#includevoidmain(){intm,n,m1,n1,t,k;printf(“请输入两个正整数:”);scanf(“%d%d”,&m,&n);if(mvoidmain(){inti,sum=0;for(i=0;i<=100;i++)if(i%2!=0)sum=sum+i;printf("sum=%d ",sum);}32、输出所有200-400之间能被3整除且个位数字为7的整数。#includevoidmain(){inti;for(i=200;i<=400;i++)if((i%3==0)&&((i-7)%10==0))printf("%d ",i);printf(" ");}33、编程计算1*2*3+4*5*6+...+97*98*99的值。#includevoidmain(){inti,n,sum=0;for(i=1;i<=94;i=i+3)sum=sum+i*(i+1)*(i+2);printf("%d ",sum);}34、输入n的值,计算并输出1*1+2*2+3*3+4*4+5*5+...+n*n的值。要求编写函数f求平方。#includevoidmain() {intf(intx);intn,sum;printf("请输n值:");scanf("%d",&n);sum=f(n);printf("%d ",sum);}intf(intx){intj,z=0;for(j=1;j<=x;j++)z=z+j*j;returnz;}35、计算并输出1-200之间不能被3整除的整数的和。#includevoidmain(){inti,sum=0;for(i=1;i<=200;i++)if(i%3!=0)sum=sum+i;printf("%d ",sum);}36、输入3个学生4门课的成绩{(60,70,65,75),(75,80,75,90),(95,75,90,65)},计算每个学生的总分和平均成绩并输出,结果保留一位小数。#includevoidmain(){inta[3][4]={{60,70,65,75},{75,80,75,90},{95,75,90,65}};inti,j;floataverage,sum=0.0;printf("三个学生的总分和平均成绩依次为: ");for(i=0;i<3;i++){sum=0.0;for(j=0;j<4;j++)sum=sum+a[i][j];average=sum/4;printf("%.1f,%.1f ",sum,average);}}37、输入一个华氏温度,输出摄氏温度,计算公式为c=5/9*(F-32)。要求结果保留两位小数。#includevoidmain(){floatF,c;printf(“请输入一个华氏温度:”); scanf(“%f”,&F);c=5.0/9*(F-32);printf("%.2f ",c);}38、输入任意三个数,按从大到小的顺序输出。#includevoidmain(){inta,b,c,t;printf(“请输入任意三个数:”);scanf(“%d%d%d”,&a,&b,&c);if(amain(){floatx=1.0;floata=0.01,b=0.02,c=0.05;inti,j,k;intcount=0;printf("1分,2分,5分张数依次为: ");for(i=1;i<100;i++)for(j=1;j<50;j++)for(k=1;k<20;k++)if((i*a+j*b+k*c)==1&&i+j+k==50){printf("%d%d%d ",i,j,k);count++;} printf("共有%d种方案 ",count);}40、从键盘输入圆的半径,计算并输出圆面积与周长,要求结果保留两位小数。#includevoidmain(){intr;floatpi=3.14159;floatc,s;printf(“请输入半径r:”);scanf(“%d”,&r);s=pi*r*r;c=2*pi*r;printf("面积=%.2f,周长=%.2f ",s,c);}41、从键盘输入一个字符串,再输入两个正整数m和n,输出字符串中从m开始,连续n个字符。例如,输入abcdefg,2,3,输出bcd。#include#includevoidmain(){inti,m,n;chara[20];printf("请输入字符串:");gets(a);n=strlen(a);printf("请输入两个正整数:");scanf("%d%d",&m,&n);for(i=m-1;ivoidmain(){inta,b,c,d,e;longx;printf(“请输入一个不多于5位的正整数:”);scanf(“%d”,&x);a=x/10000;b=x%10000/1000;c=x%1000/100;d=x%100/10;e=x%10;if(a!=0)printf("它是5位数,%d,%d,%d,%d,%d ",e,d,c,b,a);elseif(b!=0)printf("它是4位数,%d,%d,%d,%d ",e,d,c,b); elseif(c!=0)printf("它是3位数,%d,%d,%d ",e,d,c);elseif(d!=0)printf("它是2位数,%d,%d ",e,d);elseprintf("它是1位数,%d ",e);}43,输出Fibonacci数列的前40项#includevoidmain(){inta[40]={1,1};inti;printf("%12d%12d",a[0],a[1]);for(i=2;i<40;i++){if(i%4==0)printf(" ");a[i]=a[i-1]+a[i-2];printf("%12d",a[i]);}printf(" ");}44,判断某一年是否是闰年#includevoidmain(){intyear,leap;printf(“请输入一个年份:”);scanf("%d",&year);if(year%4==0){if(year%100==0){if(year%400==0)leap=1;elseleap=0;}elseleap=1;}elseleap=0;if(leap)printf("%disaleapyear ",year); elseprintf("%disnotaleapyear ",year);}45,起泡法(选择法)对十个数排序#includevoidmain(){inti,j,t;inta[10];printf("请输入10个数: ");for(i=0;i<10;i++)scanf("%d",&a[i]);for(j=0;j<9;j++)for(i=0;i<9-j;i++)if(a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}for(i=0;i<10;i++)printf("%d",a[i]);printf(" ");}46,判断某个数是否为素数.#include#includevoidmain(){inti,m;floatk;printf("请输入一个正整数:");scanf("%d",&m);k=sqrt(m);for(i=2;i<=k;i++)if(m%i==0)break;if(i>k)printf("%disaprimenumber ",m);elseprintf("%disnotaprimenumber ",m);}47,编程计算1!+2!+3!+…+10的值。 #includevoidmain(){inti,j,sum1=1,sum=0;for(i=1;i<=10;i++){for(j=1;j<=i;j++)sum1=sum1*j;sum=sum+sum1;}printf("1!+2!+3!+…+10!=%ld ",sum);}48,求以下数列前十五项的和:2/1,3/2,5/3,8/5,13/8,21/13…。#includevoidmain(){intn;floata=2.0,sum=0.0,b=1.0,t;for(n=1;n<=15;n++){sum=sum+a/b;t=a;a=a+b;b=t;}printf("%f ",sum);}49,从键盘输入圆柱体高与底面半径,计算并输出圆柱体表面积与体积,要求结果保留两位小数。#includevoidmain(){inth,r;floatpi=3.14159,s,v;printf(“请输入圆柱体半径r和高h:”);scanf(“%d%d”,&r,&h);s=2*pi*r*r+2*pi*r*h;v=pi*r*r*h;printf("s=%.2f,v=%.2f ",s,v);}50,从键盘输入一个十进制整型数据,计算并输出其各位上数字之和(忽略正负号)。例如,输入1234,输出10;输入-1234,输出10。#include#includevoidmain(){inti1,i2,i3,i4,n,sum;inta;printf("请输入一个整数:"); scanf("%d",&a);n=abs(a);i1=n/1000;i2=n%1000/100;i3=n%100/10;i4=n%10;sum=i1+i2+i3+i4;printf("sum=%d ",sum);}51,已知银行定期存款利率为r=2.25%,输入存款本金x、存款年数n,输出本利之和y=x(1+r)(1+r)...(1+r),共n个(1+r)。#includevoidmain(){floatx,y,r=0.0225;inti,n;printf("请输入存款本金x和存款年数n:");scanf("%f%d",&x,&n);y=x;for(i=1;i<=n;i++)y=y*(1+r);printf("%f ",y);}52,编程判断输入的字符串是否为“回文”。所谓“回文”是指顺读和倒读都一样,如abcba。#include#includevoidmain(){inti,n;chara[20];printf("请输入字符串:");gets(a);n=strlen(a);for(i=0;i=n)printf("是回文. "); }53、编写程序,将用户输入的字符串中删除所有的数字,然后输出剩余的字符。#includevoidmain(){inti,j,n1;chara[20];printf("请输入字符串:");gets(a);n1=strlen(a);for(i=0;i='0'&&a[i]<='9')==0)printf("%c",a[i]);printf(" ");}