admin 管理员组文章数量: 1086019
2024年3月19日发(作者:accessround函数怎么用)
《C语言程序设计》
阅读程序写结果试题
汇总
阅读程序写结果试题
第四章选择结构(共20道题)
1.
(于蕾)
#i nclude
void mai n()
{
int x,y,t; x=7;y=9; if(x { t=x;x=y;y=t;} printf("%d,%dn", x,y ); } 运行结果: 9,7 2. (于蕾) #i nclude void mai n() { int x=1,a=2,b=3; switch(x) { case 1: a--; break; case 2: b++; break; case 3: a++;b++; } prin tf("na=%d,b=%dn",a,b); } 运行结果: a=1,b=3 3. (于蕾) #i nclude void mai n() { char chi = 'E'; if(ch1 >= 'A') ch1++; else ch1+=32; printf("ch1 = %cn", ch1); } 运行结果: ch 仁 F 4. (于蕾) #i nclude void mai n() { int x,y,t; x=5;y=3; if(x>y) { t=x;x=y;y=t;} prin tf("%d,%dn" , x,y ); } 运行结果: 3,5 5. (王伟) #i nclude int main() { int a,b,c,m; prin tf("E nter three in tegers:"); sca nf("%d%d%d",&a,&b,&c); if(a<=b) m=a; else m=b; if(c m=c; prin tf("m=%dn",m); return 0; } 输入: 21 22 23< 回车 > 运行结果: m=21 6. (王伟) #i nclude int main() { char ch1='a',ch2='B',ch3='E'; if(ch1>ch2) if(ch2>ch3) ch3++; else --ch3; prin tf("ch3=%cn",ch3); return 0; } 运行结果: ch3=D 7. (王伟) #i nclude int main() { float x,y; sca nf("%f", &x); switch((i nt)x/10) { case 0: y=1.0; prin tf("y=%fn",y);break; case 1: y=2*x+1; prin tf("y=%fn",y);break; case 2: y=3*x*x+2; prin tf("y=%fn",y);break; default: prin tf("No defin itio n.n"); } return 0; } 输入: 15.3 < 回车 > 运行结果: y=31.600000 8. (王伟) #i nclude int main() { char ch 1= 'A',ch2='B'; switch(ch1) { case 'A': switch(ch2) { case 'B': prin tf("Good!n");break; case 'A': prin tf("Better!n");break; } case 'B': prin tf("Best!n"); break; } return 0; } 运行结果: Good! Best! 9. (王锋) #i nclude void mai n() { float score; score = 100; if (score<60) prin tf("En"); else switch( ( int ) score / 10 ) { case 10: case 9: prin tf("An"); case 8: prin tf("Bn"); case 7: prin tf("Cn"); break; case 6: prin tf("Dn"); break; default: tf("Errorn"); } } prin 运行结果: A B C 10. (王锋) #i nclude void mai n() { int i=0 , a=2 ; if(i==0) printf( “ **) ” else printf( “ $$ ” printf( } 运行结果: 11. (王锋) #i nclude void mai n() { int m=10, n=0,p=4,q=20; if (m) if (n) q=15-m; else q=25+m; else if (p) q=q+p; printf( q=%dn”,q); } 运行结果: q=35 12. (王锋) #i nclude void mai n() { int a=1,b=0; switch(a) { case 1: switch (b) *) ”“ { case 0: printf( **0** ” ); break; case 1:printf( “ *1** ”);break; } case 2: printf( “ *2** ” ) ;break; } } 运行结果: **0****2** 13. (宋昕) #i nclude int main() { int a=2,b=7,c=5; switch (a>0){ case 1: switch (b<0){ case 1: prin tf("@"); break ; case 2: prin tf("!"); break ; } case 0: switch (c==5){ case 1: prin tf("*") ; break ; default : printf("#") ; break ; } default : printf("&"); } prin tf("n"); return 0; } 运行结果: *& 14. (宋昕) #i nclude int main() { int a=0,b=1,c=0,d=20; if(a) d=d-10; else if (!b) if(!c) d=15; else d=25; prin tf("d=%dn",d); return 0; } 运行结果: d=20 15. (宋昕) #i nclude int main() { int a=1; switch(a){ case 0: prin tf("**0**");break; case 1:pri ntf("**1**");break; case 2:pri ntf("**2**");break; } return 0; } 运行结果: **1** 16. (宋昕) #i nclude { int x, y; scan f("%d", &x); y = x>12 ? x + 10 : x - 12; prin tf("%dn", y); return 0; } 输入: 12< 回车 > 运行结果: 0 17. (宋昕) #i nclude int main() { float x, y; char op; double r; scan f("%c%f%f",&op, &x, &y); switch(op) { case '+': r = x + y; break; case '-': r = x - y; break; case '*': r = x * y; break; case '/': r = x / y; break; } prin tf("%.1f", r); return 0; } 输入: 3.5< 回车 > 运行结果: 0.6 18. (王勇超) #i nclude "stdio.h" void mai n() { int a=7; if(a>9 || a<10) a=a/3; else a=a%3; prin tf("a=%d", a); } 运行结果: a=2 19. (王勇超) #i nclude void mai n() { int a=20; switch(a%3) { case 0: prin tf("0"); case 1: prin tf("1"); case 2: prin tf("2"); default: prin tf("3"); } prin tf("4"); } 运行结果: 234 20. (王勇超) #i nclude { int a=2,b=5,c=8,t; if ( a < b ) { t = a; a = b; b = t; } if ( b < c ) { t = b; b = c; c = t; } if ( a < b ) { t = a; a = b; b = t; } printf( "%d,%dn", a++, --c ); } 运行结果: 8,1 第五章循环结构共20道题 1. (于蕾 ) #i nclude void mai n() { int nu mber , digit; number = 1234; while ( nu mber != 0 ) { digit = nu mber%10 ; prin tf( "%d" , digit ); nu mber = nu mber / 10 ; } } 运行结果: 4321 2. (于蕾 ) #i nclude #defi ne N 5 void mai n() { int i; for(i=1;i } 运行结果: 1 4 9 16 3. (于蕾 ) #i nclude void mai n() { int i,s,x; s=0; x=15; for(i=1;i<=5;i++) if (x%i==0) s= s + i; prin tf("i=%d,s=%dn",i,s); } 运行结果: i=6,s=9 4. (于蕾 ) #i nclude void mai n() { int coun ter=1; while(co un ter <= 5) { prin tf("%d n", cou nter); coun ter++; } } 运行结果: 1 2 3 4 5 .(王伟) #i nclude int main() { int i,sum,m,a; sum=O; m=5; for(i=0;i<=3;i++) { scan f("%d",&a); if(a>m) sum+=a; } prin tf("sum=%dn",sum); return 0; } 输入: 2 10 8 3< 回车 > 运行结果: sum=18 6. (王伟) #i nclude int main() { int i,j,k; for(i=1;i<=4;i++) { for (j=1;j<5-i;j++) printf(""); for(k=1;k<=i;k++) prin tf("*"); prin tf("n"); } return 0; } 运行结果: 7. (王伟) #i nclude int main() { int i,j; i=1; while(i<5) { for(j=1;j<2*i+1;j++) prin tf("%c",#); prin tf("n"); i++; } return 0; } 运行结果: ## #### ###### ######## 8. (王伟) #i nclude int main() { int i=10,m=0, n=0; do { if(i%2!=0) m=m+i; else n=n+i; i--; }while(i>=0); prin tf("m=%d, n=%dn",m, n); return 0; } 运行结果: m=25, n=30 9. (王锋) #i nclude void main() { int sum=0,n; scan f("%d",&n); while( n<=5) { sum+=n; n++; } prin tf("sum=%d",sum); } 输入: 1< 回车 > 运行结果: sum=15 10. (王锋) #i nclude void mai n() { int i, j; for(i=2;i>=0;i--) { for(j=1;jv=i;j++) prin tf("*"); for(j=0;j<=2-i;j++) prin tf("!"); prin tf("n"); } } 运行结果: **! *!! !!! 11. (王锋) #i nclude { int a,b; for(a=1,b=1;a<=100;a++) { if(b>20) break; if ( b%4==1) { b=b+4; con ti nue; } b=b-5; } prin tf("a=%dn",a); } 运行结果: a=6 12. (王锋) #i nclude void mai n() { char k; int i; for(i=1;i<3;i++) { scan f("%c",&k); switch(k) { case '0': prin tf("a no ther n"); case '1': prin tf(" nu mber n"); } } } 输入: 01< 回车 > 运行结果: ano ther nu mber nu mber 13. (宋昕) #i nclude int main() { int i, s = 0; for(i = 1; i < 10; i++) { s += i * i; if(s > 10) break; } printf("i=%d, s=%dn", i, s); return 0; } 运行结果: i=3, s=14 14. (宋昕) #i nclude void mai n() { char ch; while((ch=getchar())!='n') { if (ch>='A '&&ch<='Z') ch=ch+32; else if (ch>='a'&&ch<='z') ch=ch-32; prin tf("%c",ch); } } 输入: ABCdefv 回车 > 运行结果: abcDEF 15. (宋昕) #i nclude { int a, b; for (a = 1, b = 1 ; a <= 100 ; a++) { if (b >= 9) break; if (b % 3 == 1) { b += 3 ; con ti nue ; } b -= 5; } printf("%d,%dn", a, b); return 0; } 运行结果: 4,10 16. (宋昕) #i nclude int mai n () { int i = 0,j = 0; while (i < 10) i++; while (j++ < 10); prin tf("i=%d,j=%dn",i,j); return 0; } 运行结果: i=10,j=11 17. (王勇超) #include "stdio.h" void mai n() { int i,j,t; for(i=1,j=10;i<3;i++,j--) t=i+j; t++; prin tf("%d,%d",j,t); } 运行结果: 8,12 18. 王勇超) #i nclude "stdio.h" void mai n() { int i=10,j; do { j = i%2; prin tf( "%d",j ); i--; } while ( i> 4 ); } 运行结果: 010101 19. (王勇超) #include "stdio.h" void mai n() { int i=7,j; while ( i> 2) { j = i%2; prin tf( "%d",j ); i--; } prin tf( "%d",i ); } 运行结果: 101012 20. (王勇超) #i nclude void mai n() { int i,j,t=O; for(i=3;i>0;i--) for(j=0;j<4;j++) t+=j; prin tf("t=%d",t); } 运行结果: t=18 第六章函数共40道题 1. (王伟) #i nclude long fun (i nt x,i nt n); int main() { int x=3 ,n=3; long p; p=fu n(x, n); prin tf("p=%ldn",p); return 0; } long fun (i nt x,i nt n) { int i; long p=1; for(i=0;i< n;i++) p*=x; return p; } 运行结果: p=27 2. (王伟) #i nclude int main() { char ch; while((ch=getchar())!='n') { if(isDigit(ch)) putchar(ch); } prin tf("n"); return 0; } int isDigit(char ch) { if(ch>='0' && ch<=9) return 1; else return 0; } 输入: Abc1d23eF45g< 回车 > 运行结果: 12345 3. (王伟) #i nclude void odddivisor(i nt n); int main() { int n; sca nf("%d",&n); odddivisor( n); return 0; } void odddivisor(i nt n) { int i; for(i=3;i<=n/2;i=i+2) if(n %i==0) prin tf("%5d",i); prin tf("n"); } 输入: 15< 回车 > 运行结果: 3 5 4. (王伟) #i nclude void prin t(); int a=5; int main() { int a=1,b=2; a=a+b; prin t(); prin tf("%d %dn",a,b); return 0; } void prin t() { int b=3; prin tf("%d %dn",a,b); } 运行结果: 5 3 3 2 5. (王伟) #i nclude void fun 2(i nt x); int main() { int x=1; x=fun 1(x); prin tf("%dn",x); return 0; } in t fun 1(i nt x) { x++; fun 2(x); return x; } void fun 2(i nt x) { x++; } 运行结果: 2 6. (王伟) #i nclude int fun 1(i nt a,i nt b,i nt c); int main() { int a=11,b=21,c=31; fun 1(a,b,c); prin tf("%d %d %dn",a,b,c); return 0; } int fun 1(i nt a,i nt b,i nt c) { a=a+10; b=b+10; c=c+10; return c; } 运行结果: 11 21 31 7. (王伟) #i nclude void fun (i nt x); int main() { fun ⑺; prin tf("n"); return 0; } void fun (i nt x) { if(x/2>1) fun( x/2); prin tf("%5d",x); } 运行结果: 3 7 8. (王伟) #i nclude void fun (i nt a[]); int main() { int i,a[5]={1,2,3}; fun( a); for(i=0;i<5;i++) prin tf("%5d",a[i]); prin tf("n"); return 0; } void fun (i nt a[]) { int i; for(i=0;i<5;i++) a[i]+=5; } 运行结果: 6 { 7 8 5 5 9. (于蕾) #i nclude int w = 5 ; fun ( w ); printf ( "n"); } void fun ( int k ) { if ( k>0 ) fun ( k-1 ); printf ( "%d" , k ); } 运行结果: 012345 10. (于蕾) #i nclude void fl(void); int a=1; void mai n() { int a=2; f1(); { int a=3; prin tf("a2=%dn",a); } prin tf("a3=%dn",a); } void f1(void) { prin tf("a1=%dn",a); } 运行结果: a1=1 a2=3 a3=2 11. (于蕾) #i nclude void f(i nt a, int b, int *c) { a=20; b=10; *c=a+b; } void mai n() { int a=10,b=20,c=30,d=40; f(a,b,&c); prin tf("%d,%d,%dn",a,b,c); } 运行结果: 10,20,30 12. (于蕾) #i nclude void swap(i nt a,i nt b); void mai n() { int a=2,b=3; prin tf("a=%d,b=%dn",a,b); swap(a,b); prin tf("a=%d,b=%dn",a,b); } void swap(i nt a,i nt b) { in t c; c=a; a=b; b=c; } 运行结果: a=2,b=3 a=2,b=3 13. (于蕾) #i nclude void fun (i nt a,i nt b,i nt c); void mai n() { int x=10,y=20,z=30; fun (x,y,z); prin tf("%d,%d,%dn",x,y,z); } void fun (i nt a,i nt b,i nt c) { a=456;b=567;c=678; } 运行结果: 10,20,30 14. (于蕾) #i nclude float f(int n) { int i;float s=0.0; for(i=1;i< n;i++) s=s+(float)1.0/i; return s; } void mai n() { int i;float a=0.0; for(i=1;i<3;i++) a=a+f(i); prin tf("a=%.4fn", a); } 运行结果: a=1.0000 15. (于蕾) #i nclude int f(i nt a); void mai n() { int a=2,i; for(i=0;i<3;i++) prin tf("%d",f(i)); } int f(i nt a) { int b=0,c=3; b++; C++ ; return(a+b+c); } 运行结果: 567 16. (于蕾) #i nclude int fun(); void mai n() { int i,x; for(i=0;i<=2;i++) x=fu n(); prin tf("%dn",x); } int fun() { int x=3; x++; return x; } 运行结果: 4 17. (王锋) #i nclude float add(float x,float y); void mai n() { float a,b,c; a=1.0; b=2.0; c=add(a,b); prin tf("%fn",c); } float add(float x,float y) { float 乙 z=x+y; return(z); } 运行结果: 3.000000 18. (王锋) #i nclude void fun (i nt x, int cp, int dp) { cp=x++; dp=++x; } void ma in (void) { int a,c=80, d=-20; a=30; fun (a,c,d); printf("%d,%dn", c,d); } 运行结果: 80,-20 19. (王锋) #i nclude void mai n() { int i=2,p; p=f(i,i+1); prin tf("%dn",p); } int f(i nt a,i nt b) { in t c; if(a>b) c=1; else if (a==b) c=0; else c=-1; return (c); } 运行结果: -1 20. (王锋) #i nclude int fun (i nt n) { if(n= =1) return 1; else return fun(n-1)+3; } void mai n() { int i,j=0; for(i=1;i<4;i++) j=j+fu n(i); prin tf("j=%dn",j); } 运行结果: j=12 21. (王锋) #i nclude int t; if(xvy) {t=x;x=y;y=t;} } void mai n() { int a=4,b=3,c=5; f(a,b); f(a,c); f(b,c); prin tf("%d,%d,%d",a,b,c); } 运行结果: 4,3,5 22. (王锋) #i nclude int age(i nt n) { in t c; if(n==1) c=10; else c=age( n-1)+2; return(c); } void mai n() { prin tf("%d",age (5)); } 运行结果: 18 23. (王锋) #i nclude void hello_world(void) { _ prin tf("Hello, world!n"); } void three_hellos(void) { int coun ter; for (co un ter =1;co un ter <= 3;co un ter++) hello_world(); } _ void ma in (void) { three_hellos(); } _ 运行结果: Hello, world! Hello, world! Hello, world! 24. (王锋) #i nclude { int x,i=1,k=3; x=f(i,k); prin tf("x=%d n",x); } int f(i nt a,i nt b) { int M=0; while(b!=0) { M=M+a; b--; } return M; } 运行结果: x=3; 25. (宋昕) #i nclude int i,s = 0; for(i = m;i < n;i++) s += b[i]; return s; } int main() { int x,a[ ] = {1,2,3,4,5,6,7,8,9}, *p = a; x = f(p,3,7); prin tf("x=%dn",x); return 0; } 运行结果: x=22 26. (宋昕) #i nclude void fun (i nt i, i nt j) { int x = 7; prin tf("i = %d; j = %d; x = %dn", i, j, x); } int main() { int i = 2, x = 5, j = 7; fun(j, 6); prin tf("i = %d; j = %d; x = %dn", i, j, x); return 0; } 运行结果: i = 7; j = 6; x = 7 i = 2; j = 7; x = 5 27. (宋昕) #i nclude void f(int a[]) { int i=0; while(a[i]<=10) { prin tf("%d",a[i]); i++; } } int mai n() { int a[]={1,7,17,9,11,34}; f(a+1); } 运行结果: 7 28. (宋昕) #i nclude void add(i nt x, int y, int z) { z = x + y; x = x * x; y = y * y; printf("(2) x = %d y = %d z = %dn", x, y, z); } int main() { int x = 2, y = 3, z = 0; prin tf("(1) x = %d y = %d z = %dn", x, y, z); add(x, y, z); prin tf("(3) x = %d y = %d z = %dn", x, y, z); return 0; } 运行结果: (1) x = 2 y = 3 z = 0 (2) x = 4 y = 9 z = 5 (3) x = 2 y = 3 z = 0 29. (宋昕) #i nclude nt x, int y) { x1 = x; x = y; y = x1; } int main() { int x3 = 10, x4 = 20; sub(x3, x4); sub(x2, x1); prin tf(" %d, %d, %d, %dn", x3, x4, x1, x2); return 0; } 运行结果: 10, 20, 40, 40 30. (宋昕) #i nclude int x; void cube() { x = x * x * x; } int main() { x = 5; cube(); printf(" %dn", x); return 0; } 运行结果: 125 31. (宋昕) #i nclude in vert(i nt *s, int i, i nt j) { int t; if(i < j) { inv ert(s, i + 1,j - 1); t = *(s + i); *(s + i) = *(s + j); *(s + j) = t; } } void mai n() { int a[6] = {10, 6, 23, -90, 0, 3}, i; in vert(a, 0, 5); for(i = 0; i < 6; i++) printf("%d, ", a[i]); prin tf("n"); } 运行结果: 3,0,-90,23,6,10 32. (宋昕) int func(int a[][3]) { int i,j,sum=0; for(i=0;i<3;i++) for(j=0;j<3;j++){ a[i][j]=i+j; if(i==j) sum = sum+a[i][j]; } return sum; } int main() { int a[3][3]={1,3,5,7,9,11,13,15,17}; int sum; sum=fun c(a); prin tf("sum=%d",sum); return 0; } 运行结果: sum=6 33. (王勇超) # in elude void fun(); void mai n() { int i; for(i=2; i>0; i--) fun (); } void fun() { i*=2; prin tf("i=%dn", i); } 运行结果: i=20 i=40 34 .(王勇超) #i nclude int fun (i nt n) { if(n<=1) return 1; else return fun(n-1)* n; } void mai n() { int i,j=0; for(i=1;i<=3;i++) j+=fu n(i); prin tf("j=%dn",j); } 运行结果: j=9 35. (王勇超) #i nclude int fun a(i nt a, int b) { int t; if (a > b) t = a/b; else t = a%22; return t; } void mai n() { int a=33, b=22,c; c=fun a(a,b); prin tf("c=%dn", c); } 运行结果: c=1 36. (王勇超) # in clude void fun (i nt i); void mai n() { int i; for(i=2; i>0; i--) fun( i); } void fun (i nt i) { i*=2; prin tf("+%d+", i); } 运行结果: +4++2+ 37. (王勇超) # in elude void fun (i nt n); void mai n() { int x=879; fun( x); } void fun (i nt n) { int a,b,c,t; a = n % 10; b = (n/10) % 10; c = (n/100) % 10; if ( a < b ) { t = a; a = b; =t; } if ( b < c ) { t = b; b = b : c; c :t; } = =t; } if ( a < b ) { t = a; a = b; b : prin tf( "%d", b ); 运行结果: 8 38. (王勇超) #i nclude { int t; if (a > b) { t = a; a = b; b = t; } return t; } void mai n() { int a=33, b=22,c; c=fun a(a,b); printf("c=%dn", c); } 运行结果: c=33 39. (王勇超) #i nclude int swap(i nt x, int y) { int 乙 z=x%10; x=y%10; y=z ; z=x; return 乙 } void mai n() { int a=38, b=29,c; c=swap(a,b); prin tf("%d%dn", a,c); } 运行结果: 389 40. (王勇超) #i nclude void fun (i nt n) { if(n==1) prin tf("* n"); else { prin tf("-"); fun(n-1); } } void mai n() { int i,j=0; for(i=1;i<4;i++) fun( i); prin tf("Xia oXi n!"); }
版权声明:本文标题:《C语言程序设计》阅读程序写结果试题汇总培训资料 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1710783960a572940.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论