《信息科学与工程学院C语言上机150道题参考答案.pdf》由会员分享,可在线阅读,更多相关《信息科学与工程学院C语言上机150道题参考答案.pdf(133页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、信息科学与工程学院C 语言上机150道题参考答案001#include#includemain()(float a10,ave,max,min,sum=0;inti;printf(”请输入10个考生的成绩:n);for(i=0;i10;i+)scanf(%f,&ai);for(i=0;i10;i+)sum+=ai;ave=sum/10;max=a0;for(i=1;imax)max=ai;min=a0;for(i=1;i10;i+)if(aimin)max=ai;printf(平均成绩:%f,最高分:%f,最低分:%f,ave,max,min);getch();)002#include#inc
2、ludemain()(int a,b;printf(输入两个整数:n”);scanf(%d%d,&a,&b);printf(这两个整数的商是:%.5f,(float)a/b);getch();)003#include#includemain()(int i=33;while(i=127)printf(H%c J);i+;getch();)004#include#includemain()(int i=33;do(printf(%c”,i);i+;while(i=127);getch();)005#include#includemain()int i;for(i=33;i=127;i+)prin
3、tf(H%c”,i);getch();)006#include#includemain()(int i;for(i=1;i=500;i+)(if(i%3=2&i%5=3&i%7=5)printff%dnIi);)getch();)007(方法一)#include#include#includemain()(char a100;int len.i;printf(请输入一个整数scanf(H%sH,a);len=strlen(a);for(i=len-1;i=0;i-)printfC%cM,ai);getch();)007(方法二)#include#includemain()(int num,t;
4、printf(请输入一个整数:n);scanf(n%d,&num);while(num0)t=num%10;num=num/10;printfC%d,t);)getch();)008#include#includemainQ(int n;float t=1,s=0;for(n=1;t0.0001;n+=2)(t=(float)1/n;s=s+t;)printf(s=%r,s);getch();)009#include#includemainQ(int nj,s=O;printf(请输入一个整数:n”);scanf(,%d,&n);for(i=1;i=n;i+)s=s+i*i;printf(%d
5、,s);getch();)010#include#includemain()int s=0,i=1;while(i=100)s=s+i;i+;)printf(%d,s);getch();)11.#includemain()(int i,result;i=1;result=O;while(i=100)(result=i+result;i+;)printfC,1+2+.+100=%d,result);while(1);)12.#includemain()(int i,result;for(i=1,result=0;i=100;i+)(result=i+result;)printf(1+2+.+10
6、0=%d,result);while(1);)13.o#includelong fac(int n)(long result;if(n=0|n=1)result=1;else result=n*fac(n-1);return result;)mainQint n;long result;printf(“请输入一个小于等于10的整数n”);scanf(%d,&n);if(n=0&n=10)(result=fac(n);printf(%d!=%ld,n,result);)else printf(输入数据错误”);while(1);)14.#includelong fac(int n)(long r
7、esult;if(n=0|n=1)result=1;else result=n*fac(n-1);return result;)mainQint n,m;long result;printf(“请输入两个小于等于10的整数n”);scanf(%d%d,&n,&m);if(n=0&n=0&n=10)(result=fac(n)+fac(m);printf(%d!+%d!=%ld,n,m,result);)else printf(输入数据错误”);while(1);)15.#includemain()(int i,result;for(i=1,result=0;i=100;i+)(if(i%3=0
8、)result=result+i;)printf(1-100之间的所有3 的倍数之和为d,result);while(1);)16.#includemain()(int i,result;for(i=100,result=0;i=200;i+)(if(i%7=0)result=result+i;)printf(100-200之间的所有7 的倍数之和为d,result);while(1);)17.o#includemain()int i,num;for(i=1,num=0;i=100;i+)(if(i%7=0|i%10=7)num+;)printf(1-100之间,7的整数倍数或数字尾数为7的数
9、字个数为%d”,num);while(1);)18.o#include int main()(int i=0,space=0,num=0,n=0,ch=0;char s1000000;printf(请输入一串字符);gets(s);while(si!=0)(if(si=)space+;else if(si=0)num+;else if(si=a|si=A)ch+;elsen+;i+;)printf(刚才输入的字符中英文字符个数为%dn,ch);printf(刚才输入的字符中空格个数为%dn,space);printf(刚才输入的字符中数字个数为%dn,num);printf(刚才输入的字符中其
10、他个数为%dn,n);while(1);)19.o#includemain()(int n,i,j;i=1;j=0;printf(请输入一个整数n);scanf(d”,&n);if(n=O)printf(“这个数是 1 位数J);else(while(n=i)(i=10*i;j=j+1;)printf(这个数是d 位数”,j);)while(1);)20.#includemain()(printf(1到 5 0 中间所有能被3 整除的正整数如下n“);int n;for(n=1;n=50;n+)(if(n%3=0)printfC%dt,n);)while(1);)21.o#includemai
11、n()(printffl00-200之间不能被3 整除的正整数如下rT);int n;for(n=100;n=200;n+)(if(n%3!=0)printf(%dt,n);)while(1);)22.#includemainQ(printf(Y=);int n,result;for(n=1,result=0;n=111;n=n+2)printf(%d+,n);result=result+n;)pri ntf(*-%dn,res u It);while(1);)23#include#includemain()(inti;long int y;y=1;for(i=1;i=10;i+)(y=y*i
12、;)printf(y=1/%ld,y);getch();)24#include#includemain()(int i,j;for(i=1;i=9;i+)(f0 r(j=1;j=i;j+)printf(%d*%d=%dt,j,i,i*j);printf(nn);)getch();25#include#includemainQint i=O,n;for(n=1;(2*n-1)100;n+)i=i+(2*n-1);printf(1到100以内的奇数之和为:%dn”,i);getch();)26#include#includemainQ(int i,s;s=0;for(i=2;i=100;i=i+2
13、)s=s+i;printf(1到100的偶数和是到;printf(s=%dn,s);getch();)27#include#include#includeint isprime(int m)int i;for(i=2;i=(int)sqrt(m)+1)return 1;)main()int n.flag;for(n=1;n=200;n+)(flag=isprime(n);if(flag)printf(%dt,n);getch();)28#include#includemain()(float a,t;printf(Please input the valuet);scanf(%r,&a);if
14、(a0)t=-a;elset=a;printf(The absolute value is:t%f,t);getch();)29#include#includemain()(char ch;ch=getchar();if(ch=,+)printf(plusn);else if(ch=,-)printf(minusn);else jf(ch=w)printf(multiplyn,);else if(ch=7)printf(dividen);elseprintf(you are wrong);system(npause);)30#include#includemain()(char n1=a,n2
15、=Kn3=4;char m1=A,m2=R,m3=F;printf(请输入一个字符scanf(%c,&n4);if(n4=n1|n4=m1)printf(you are right!n);printf(n4=%cn,n4);else if(n4=n2|n4=m2)printffyou are right!n);pri ntf(n4=%cn,n4);)else if(n4=n3|n4=m3)printf(Hyou are right!n);printf(n4=%cn,n4);)elseprintf(Hyou put into a wrong char!n);getch();)31#include
16、#include#includemain()double a,b,c;double s,x1,x2;printffplease enter a,b,c:rT);scanf(,%lf%lf%lf,&a,&b,&c);if(a=-(1 e-6)&a(1e-6)(x1=(-b+sqrt(s)/(2*a);x2=(-b-sqrt(s)/(2*a);printf(there are two different real:nx1=%5.2f,x2=%5.2fn,x1,x2);elseif(s=-(1e-6)&s=(1e-6)(x1=x2=-b/(2*a);printf(there are two equa
17、l real:nx1=x2=%5.2fn,x1);)elses=-s;x1=-b/(2*a);x2=fabs(sqrt(s)/(2*a);printf(there are two different complex:n);printf(Hx1=%5.2f+%5.2fi,x2=%5.2f-%5.2fin,x1,x2,x1,x2);)getch();)32#include#includemain()(int score,temp,logic;char grade;logic=1;while(logic)(printf(input score:n);scanf(M%dn,&score);if(sco
18、re 100)|(score0)printf(nerror,input again!nH);elselogic=0;printfOn);)if(score=100)temp=9;elsetemp=(score-score%10)/10;switch(temp)(case 9:grade=A;break;case 8:grade=B;break;case 7:grade-C;break;case 6:grade=d;break;case 5:case 4:case 3:case 2:case 1:case 0:grade=e;break;printf(score is:%d,the grade
19、is:%c.n,score,grade);system(pause);)33#include#includemain()(int x;printf(Please enter x:*);if(x%3=0)&(x%5=0)&(x%7=0)printf(%d 能被 3,5,7 整除”,x);elseprintf(%d 不能被 3,5,7 整除”,x);getch();)34#include#includemain()(int month,i;loop:printf(enter an integer(1-12):nH);scanf(%d,&month);if(month12)goto loop;r=m
20、onth%12;r=r/3;if(r=O)printf(wintern);else if(r=1)printf(springn);else if(r=2)printf(summern);elseprintf(aiitumnn);getch();)35#include#includemain()(int a,b,c;printf(please enter a,b,c:n);scanf(%d%d%d,&a,&b,&c);switch(a)(case 1:printf(%d+%d=%dn,b,c,b+c);break;case 2:printf(%d-%d=%dn,b,c,b-c);break;ca
21、se 3:printf(%d*%d=%dn,b,c,b*C);break;case 4:printf(%d/%d=%dn,b,c,b/c);break;default:break;)getch();)36#include#includemain()(int a;printf(请输入整数A:n);scanf(%d,&a);if(a=10)&(a=100)&(a999)p intf(整数%d大于等于100且小于1000。”,a);elseprintf(”整数%(1大于等于 1000。”,a);getch();)37#include#includemain()int a;printf(请输入整数a:
22、n);scanf(%d,&a);switch(a)(case 65:printf(AH);break;case 66:printf(B);break;case 67:printf(C);break;default:printf(END);)getch();38#include#includemain()(int a,b,c,d,t;printf(请输入 4 个整数:a,b,c,d:n);scanf(%d%d%d%d,&a,&b,&c,&d);if(ab)t=a;a=b;b=t;if(cd)(t=d;d=c;c=t;)if(ac)(t=c;c=a;a=t;)if(bd)(t=b;b=d;d=t;
23、)if(bc)t=b;b=c;c=t;)printf(”排 序 后:%d%d%d%d,a,bJc,d);getch();)39#include#includemain()(int s,p;float tax;printf(enter income:);scanf(%d,&s);printf(n);if(s800)if(s-800)%100!=0)p=(s-800)/100+1;elsep=(s-800)/100;switch(p)case 0:case 1:case 2:case 3:case 4:tax=(s-800)*0.05;break;case 5:tax=400*0.05+(s-12
24、00)*0.08;break;default:tax=400*0.05+800*0.08+(s-2000)*0.2;break;)else tax=0;printf(,Tax=%.2f,tax);getch();)40#include#includemain()(int score,temp,logic;char grade;logic=1;while(logic)(printf(Hinput score:);scanf(%d,&score;);if(score 100|score0)printf(nerror,input again!n);elselogic=0;printf(n);)if(
25、score=100)temp=9;elsetem p=(score-score%10)/10;switch(temp)case 9:grade=,A*;break;case 8:grade=B;break;case 7:grade=C;break;case 6:grade=D;break;case 5:case 4:case 3:case 2:case 1:case O:grade=E;)printf(score is:%d,the grade is:5c.n,score,grade);getch();#includeint main(void)int x,i=O;doprintf(请输入数字
26、:);scanf(%d,&x);if(x99999|x99999|x0);while(x!=0)(printf(%dt,x%10);x=x/10;i+;)printf(n 这个数共有%d 位n,i);return 0;)42#include#includedouble square(int a,int bjnt c);main()int x,y,z;float S;printf(please enter x,y,and z(x,y,z0)n);scanf(%d%d%d,&x,&y,&z);if(x+y=z)|(x+z=y)|(y+z=x)printf(sorry,you are wrong!n
27、H);ElsePrintf(yes!n);43#include#includemain()(int a,b,c;printf(Hplease enterAn);scanf(%d%d,&a,&b);if(a=b)c=a*b;printf(%d*%d=%dn,a,b,c);else(c=b/a;printf(%d/%d=%d,a,b,c);)getch();)45#include#includevoid main()(int a,b,c;printf(请输入三角形的三条边用逗号隔开a,b,c:);scanf(,%d,%d,%d,&a,&b,&c);if(a0)&(b0)&(c0)&(a+b)c)&
28、(b+c)a)&(a+c)b)(if(a*a=b*b+c*c)|(b*b=a*a+c*c)|(c*c=a*a+b*b)printf(直角三角形n);elseprintf(不是直角三角形n);elseprintf(不能组成三角形n);)46#includemain()(int a,b,c;printf(请输入三角形的三条边用逗号隔开a,b,c:);scanf(%d,%d,%d,&a,&b,&c);if(a+b)c&(a-b)c)if(a=b|b=c|c=a)printf(这是个等腰三角形n”);elseprintf(这不是等腰三角形n”);elseprintf(“这三条边不能构成三角形n”);)
29、49#includemain()int i,j=O,a;printf(请输入一个整数n);scanf(%d,&a);if(a0&a!=1)(for(i=1;i2)(printf(这不是素数n);)else(printf(这是素数n);)if(a0)printf(输入错误);if(a=1|a=0)printf(这不是素数)50#includeint flag(char a,char b);main()(int i,j;char a3=A,B,C;char b3=,X,Y,Z;for(i=0;i=2;i+)for(j=0;j=2;j+)(if(flag(ai,bO)=0)0elseprintf(%
30、c%cn,ai,bj);)int flag(char a,char b)if(a=A&b=X,)return 0;else if(a=C&b=X)return 0;else if(a=C,&b=Z)return 0;elsereturn 1;)51.#include#includevoid main()(int a,b,c,d,e,f,t;printf(从键盘输入两个不等于零的正整数a,b(avbM“);scanf(n%d,%d,&a,&b);printf(Hn 从键盘输入两个不等于零的正整数c,d-使数据以c 的 d 次方增长n);scanf(,%d,%d,&c,&d);e=a*pow(c,
31、d);f=b*pow(c,d);t=e+f;printf(n%d 的几何级数二%dn,a,e);printf(%d 的几何级数=%d”,b,f);printf(%d,%d之间的几何级数的和二%dn”,a,b,t);)52#includemain()(char ch;unsigned count=0;printf(nPlease enter your words:n);while(ch=getchar()!=EOF)count=count+1;printf(,count=%un,count);)53#includevoid main()(int r,s;for(r=1;r100;r+)(s=3.
32、14*r*r;if(s100)printf(半径为%d,面积为 dn”,r,s);)54#includevoid main()(int i,j;for(i=1;i=9;i+)forQ=1;j=i;j+)printf(%d*%d=%dt,j,ij*j);printf(Hn);)55#includemain()(int p,r,n,m,temp;pHntf(”请输入两个正整数,用逗号隔开scanf(d,%d”,&n,&m);if(nm)(temp=n;n=m;m=temp;)p=n*m;while(m!=O)r=n%m;n=m;m=r;)printf(这两个数的最大公约数是:811”,1 1);p
33、rintf(这两个数的最小公倍数是:%dn”,p/n);)56.#includevoid main()(int i,j,sum=0;float a10,b10;for(i=0;i=5;i+)(for(j=0;j=4;j+)(if(i=0&j=0);Z/i=O,j=O时,组合出的面值是0,所以sum不 加1 ,即什么都不执行elsesum+;)for(i=1;i=5;i+)检测可能组合出的相同的面值,并减去ai=float(i*0.2);for(j=1;j=4;j+)(bj=floatd*0.3);if(ai=bj)sum-;)printf(5张 2 角和4 张 3 角的邮票共能组合出%d 种不
34、同面值的邮资!n”,sum);)57.#include#includevoid prime_number(int n)判断整数n 是否为素数(int i,m,t,s=1;/s是一个标志,s=1表示是素数,s=0表示不是素数m=sqrt(n);for(i=2;i=m;i+)t=n%i;if(t=O)s=0;)if(s=1)/如果s=1,n就是素数printf(dn”,n);)void huiwen(int n)(intj,k,a;k=n;a=0;while(k!=0)/把整数n反向表示成a,即如果n=321,反向后a=123(j=k%10;k=k/1O;a=a*1O+j;)if(a=n)如果a=
35、n,那么n就是回文数,然后再调用素数判断函数判断n是否为素数prime_number(n);)void main()inti;printf(1OOO以内的回文素数有:n);for(i=1;i=1000;i+)huiwen(i);)58.#includenumber=num;int pow(int n)while(num!=O)(inti,s=1;as=num%10;for(i=0;in;i+)num/=10;s*=10;S+;return s;)for(i=1;is;i+)void fun(int num)(bi=ai*pow(i);int i,a7,b7,c7.number,s=0,sum;
36、)for(i=1;is;i+)prim%dn”,number);ci=number%(pow(i);)sum=aO*number;for(i=1;is;i+)(sum+=bi*cs-i;)if(sum%pow(s)=number)59.include#include int prime_number(int n)判断整数n 是否为素数(int i,m,t,s=1;/s 是一个标志,s=1 表示是素数,s=0表示不是素数m=sqrt(n);for(i=2;i=m;i+)(t=n%i;if(t=O)void main()(int i,m;for(i=0;i=1):);scanf(d”,&n);wh
37、ile(n=1)for(i=1;i=primesi;i+)printf(数据不合法,请重新输入数据:();while(n%primesi=O)scanf(%d,&n);()ak=primesi;for(i=2;i=n;i+)k+;(n/=primesi;s=prime_number(i);)if(s=1)primes+j=i;for(i=0;ik-1;i+)printf(%d*,ai);count=j;printf(%dn,ak-1);printf(%d=Jn);)60.#includevoid main()(float a,grade100,average,sum=0;grade100存储不
38、及格成绩,average表示平均成绩,sum表示总成绩总人数printf(”请输入一个学生的成绩(0 1 00):);scanf(%r,&a);while(a0&a=100)要求输入成绩在0 100之间(n+;int i=O,j,n=O;/i表示不及格人数,n 表示sum+=a;if(a60)printf(H 平 均 成 绩(是:%4.2f/%d=%4.2fnu,sum,n,average);gradei=a;printf(共有%d 同学成绩不及格,他们的成i+;绩如下:n”,i);for(j=0;ji;j+)printf(请输入一个学生的成绩(0-100):printf(”4.2ft”,gr
39、adej);scanf(%f,&a);));printf(nn);average=sum/n;)61.include#include int main()(char a81;int sum=0;gets(a);for(int i=strlen(a)-1;i=0;i-)/strlen是统计字符串长度的函数,它的头文件是string.hprintf(%c,ai);printf(n);return 0;)62.#includeint fun(int n)(if(n=0|n=1)return 1;elsereturn n*fun(n-1);)void main()(int n,s;printf(”请输
40、入一个正整数:”);scanf(H%dM,&n);s=fun(n);printf(%d!=%dn,n,s);)63.编一函数,从键盘输入一个正整数n,求Xn。64.#include void Swap(int&x,int&y)(int tern;tem=x;x=y;y=tem;)int main()(int x,y;printf(”请分别输入两个整数:);scanf(n%d%d,&x,&y);int max=x*y;int t;if(xvy)Swap(x,y);t=x%y;while(t!=O)(x=y;y=t;t=x%y;)max=max/y;printf(最小公倍数为:%dn,max);r
41、eturn 0;)65.include include void number()(char a81;int i,sum=0,s;printf(请输入一个字符串:”);gets(a);s=strien(a);for(i=0;i=48&ai=57)sum+;)printf(该字符串中数字的个数为:%dn,sum);)int main()(number();return 0;)66.#include/此程序,对于如果要查找的数据在数组里,且此数据有重复,只输出查找的第一个数据的位置。如果要把所有与查找的数据都输出来,将第20行的代码return 0;去掉。int main()(int 矶 56;i
42、nt ij,x;printf(输入5行6列的数组:n);for(i=0;i5;i+)for(j=0;j6;j+)scanf(H%d,&aiO);printf(输入你要查询的数:”);scanf(n%d,&x);for(i=0;i5;i+)for(j=0;j6;j+)if(x=aig)(printf。你要查找的数在第d行,第 1列!n,i+1,j+1);return 0;)return 0;)67.include int abs_sum(int a,int b)(if(a0)return b-a;else if(a0&b0)return a-b;else if(a0&b0)return-(a+b
43、);return a+b;)int main()(int x,y,z;printf(请分别输入x,y,z的值:);scanf(d%d%d”,&x,&y,&z);printf(%dn,abs_sum(abs_sum(x,y),z);return 0;)68.include int Fibonacci(int n)(if(n=0)return 0;else if(n=1)return 1;return Fibonacci(n-1)+Fibonacci(n-2);)int main()(inti;for(i=0;i=40;i+)printf(%drT,Fibonacci。);return 0;)69
44、.include#define PI 3.14double area(double r)(return Pl*r*r;)int main()double r;printf(请输入半径:);scanf(%lf,&r);printf(%.3lfn,area(r);return 0;)70#include#include#define PI 3.14double num(double r,double h);main()(double area,r,h;scanf(%lf%lf,&r,&h);area=num(r,h);printf(%lf,area);)double num(double r,do
45、uble h)return(4*PI*r*r+2*PI*r*h);)71#include#define PI 3.1415926float area(float r,float h)(double v;v=p|*r*r*h;return v;)main()(float r,h,v;printf(请输入圆的半径和高度n);scanf(%f%f,&r,&h);v=area(r,h);printf(%f,v);getchar();getchar();)72.#includeI nt func(int n)(Return n*n*n;)main()int x,y;printfCpIease input
46、 the value of x:rT);scanf(d”,&x);y=func(x);printf(y=%d”,y);)73#include;#define N 10void swap(int a。,int bQ)()int i.temp;main()temp=ai;ai=bO;bi=temp;for(i=0;iN;i+)intj;int aN,bN;for(j=0;jN;j+)for(j=0;jN;j+)scanf(%d,&aj);for(j=0;jN;j+)scanf(H%d,&bg);swap(a,b);printfCafter change:n);75#include#define n
47、 6main()(int an,bn;int i,j,sum=O,c;printf(nplease input 6 num:*);for(i=0;in;i+)scanf(%dn,&ai);printf(n);printf(please input 6 num:);for(i=0;in;i+)printfC%d M,aO);printf(n);for(j=0;jN;j+)printf(w%d-printf”n”);for(i=0;in;i+)forO=0;jn;j+)(if(ai=bU)sum+;)printf(相等的个数:);printf(%dn,sum);c=n-sum;printf(不相等
48、的个数:printf(,%dn,c);scanf(%d,&bi);)77#include#define N 10#define M 10int andian(int aM,int n,int m)(int i,j,k,max,maxcol;int flag1,flag2=0;for(i=0;in;i+)(max=ai0;for(j=1;jmax)(max=aij;maxcol=j;)for(k=0,flag1=1;kn&flag1;k+)if(aimaxcolmax)if(flag1=1)printf(%3d%3d%5dn,i,maxcol,max);flag2=1;)if(flag2=0)r
49、eturn-1;)main()(int i,j,m,n,aNM;scanf(,%d%d,&n,&m);for(i=0;in;i+)(for(j=0;jm;j+)scanf(n%d,&aiD);flag1=0;printf(n);78.#include#includeint huiwen(char str);int main()(char str50;printf(please input strings:nH);gets(str);if(huiwen(str)printf(%s 是回文数:n”,str);elseprintf(%s 不是回文数:n,str);getchar();)int hui
50、wen(char str)(in tij,len,flag=1;len=strlen(str);for(i=0,j=len-1;i=j;i+,j-)if(stri!=strj)flag=O;return flag;80#includemainQ(int a23=1,2,3,4,5,6);int i,j;int b32;for(i=0;i2;i+)(for(j=0;j3;j+)printf(,a%d%d=%dt,i,j,aij);printf(nn);)for(i=0;i3;i+)for(j=0;j2;j+)bii=aji;for(i=0;i3;i+)for(j=0;j2;j+)printf(b