1、二级编程1、求1+2+3+.+100。(循环)答案#include void main() int i,sum=0; for(i=1;i=100;i+) sum=sum+i; printf(%d,sum); 2、 求1*2*3*.*10。(循环)答案void main() int i=0,j=1; for (i=2;i=10;i+) j*=i; printf(%d,j); return 0; 3、 输入三个数字,输出他们的最大值。(if)答案#includevoid main() int a,b,c,d; scanf(%d,%d,%d,&a,&b,&c); d=max(a,b,c); prin
2、tf(max=%d,d); getch();/*暂停看运行结果*/ int max(int x,int y,int z)int u; if(x=y&x=z) u=x; else if(y=x&y=z) u=y; else u=z; return(u); 4.用起泡法对十个数据排序(数组实现)答案#includemain ( ) int i,j,t;static int a10=5,7,4,2,3,6,1,0,9,8;for(j=0;j9;j+) for(i=0;ia) t=a;a=a;a=t ;for(i=0;i10;i+)printf(%2d,a); 5、输入十个数字,逆序输出。(数组实现)
3、答案#includemain()int a10,i=0;for(i=0;i=0;i-)printf(%f,a);6输入两个数,交换他们的值并输出。(元素交换)答案#includeint main ()int m,n,temp;scanf(%d%d,&m,&n);if (mn)temp=m;m=n;n=temp;printf(%d,m);return 0;7.输出9*9乘法表。(双层循环)答案#include void main()int i=1;for(i; i=9; i+)int j=1;for(j;j=i;j+)printf(%d*%d=%d , i, j, i*j);printf(n);
4、8.输入一行字符,将所有的小写字母转换成大写字母,大写字母转换成小写字母,其余字符不变。输出转变后的这行字符。答案#include stdio.hvoid main()char an;int i;scanf(%s,a);printf(大写为:);for(i=0;i=n;i+)if(a=a)a=a-32;printf(%c,a);printf(n小写为:);for(i=0;i=65&c=97&c=48&c=100&x1000) a=0.01*x;b=10*(0.01*x-a);c=x-100*a-10*b;if(x=(pow(a,3)+pow(b,3)+pow(c,3) printf(%5d,x
5、);x+;6.7main()int m,i,j,s;for(m=6;m10000;m+)s=1;for(i=2;im;i+)if(m%i=0) s=s+i;if(m-s=0)printf(%5d its fastors are 1 ,m);for(j=2;jm;j+) if(m%j=0)printf(%d ,j);printf(n);或main()int m,i,j,s;for(m=6;m1000;m+)s=m-1;for(i=2;im;i+)if(m%i=0) s=s-i;if(s=0)printf(%5d its fastors are 1 ,m);for(j=2;jm;j+) if(m%
6、j=0)printf(%d ,j);printf(n);6.8main()int i=1,n;double t,x=1,y=2,s,sum=0;scanf(%ld,&n);while(i=n) s=y/x;sum=sum+s;t=y;y=y+x;x=t;i+;printf(%fn,sum);11,P128 例6.10(译码)#includevoid main()char c;while(c=getchar()!=n)c=c+4;if(cZ+4|cz)c=c-26;printf(%c,c);printf(n);12,P111 5.5 5.6(switch)5.5#include main()in
7、t x,y;printf(输入x:);scanf(%d,&x);if(x1) y=x;printf(x=%3d, y=x=%dn,x,y);else if (x10) y=2*x-1;printf(x=%3d, y=2*x-1=%dn,x,y);else y=3*x-11;printf(x=%3d, y=3*x-11=%dn,x,y);5.6#include main() float score;char grade;case 2:printf(请输入学生成绩:);scanf(%f,&score);while(score100|(score0) printf(n输入有误,请重新输入:);sca
8、nf(%f,&score);switch(int)(score/10) case 10: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 1:case 0: grade=E;printf(成绩是%5.1f,相应的等级是%c。n,score,grade);13,P108 例5.5(一元二次方程求根) 例5.6(求闰年)5.5#includevoid main()int year,leap;scanf(%d,&year);
9、if(year%4=0)if(year%100=0)if(year%400=0)leap=1;elseleap=0;elseleap=1;elseleap=0;if(leap)printf(%d is,year);elseprintf(%d is not,year);printf(a leap year.n)5.614,P31 例2.17 例2.182.17输出50个学生中成绩高于80分者的学号和成绩2.18输出20002500年每一年是否闰年#include void main() int year; year=2000; go: if(year%4 = 0)&(year%100 != 0) | (year%400 = 0) printf(%d is run nian,year); if(year2500)goto end; goto go; end: getch();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1