1、C语言答案1、477542、二进制:10110001八进制:261十六进制:B13、01111111111111114、11111111 111111105、896、010010017、100101008、-399、1110011010、0000100011、t=0 改为 t=1; in 改为 i=n; t=t*1改为 t=t*I;12、#include void main()printf(“ * n“); printf(“ * * n“); printf(“* *n”); printf(“ * * n“); printf(“ * n“);13、#include void main() int
2、 a; a= 2+3+4; printf(“%d”,a);1. (1) = sin(x) * sin(x) * ( a + b ) / ( a b )(2) x 20 & x 30 | x 42.(1) 2.5+-1=3.5(2) 1+0=1(3) 03n = m % 10 + m / 10 % 10 * 100 + m / 100 * 10 /*n为bac , m为abc*/4(1):1(2):1(3):1(4):3(5):1(6):0(7):0(8):65(C = 65 & C =90)? (C = C) : (C = C + 32)第三章1#include void main() flo
3、at pi , r , l , s; pi = 3.14; /*定义PI的值*/ printf(Please input the r:n); scanf(%f,&r); l = 2 * pi * r; /*计算周长*/ s = pi * r * r; /*计算面积*/ printf(周长= %.2fn面积 = %.2fn,l,s);运行结果:Please input the r:5周长 = 31.40面积 = 78.502#include #include void main() int i=0; char c; printf(输入一串字符); while(c = getchar() != n
4、) /*利用getchar的特性进行对c的逐个赋值*/ i+; putchar(c); printf(n位数为:%dn,i);运行结果:输入一串字符chinachina位数:53#include void main() int a , b , x , y; printf(请输入头数 脚数:n); scanf(%d %d,&a,&b); if ( b % 2 = 0 & b = 2 * a) x = (b - 2 * a) /2; y = a - x; printf(鸡数:%dn兔数:%dn,y,x); else printf(Data is error);运行结果:请输入头数 脚数:24 64
5、鸡数:16兔数:84#include#includevoid main() char a , b , c; printf(正序输入为:); a = getche(); b = getche(); c = getche(); /*顺序读入三个字符*/ printf(n逆序输出为:); putchar(c); putchar(b); putchar(a); printf(n); /*倒序输出三个字符*/第四章1算术运算是有加,减,乘,除组成的表达式; 关系运算是有,=,=,!=等关系运算符组成的表达式; 逻辑运算有与(&),或(|),非(!)等逻辑运算符组成的表达式!21 1 1 0 0 130
6、1 0 14C语言中用1表示“真”,用0表示“假”当遇到非零数时,系统判断为真;遇到零时,为假5#include void main() int x; printf (Please input xn); scanf (%d,&x); if ( x = 0 ) x = x; else x = -x; printf (Absolutely is %dn,x);运行结果:Please input x-359Absolutely is 359运行结果:正序输入为:956逆序输出为:6595#include #include void main() float a , b , c , s , area;
7、 printf(Please input:n); scanf(%f %f %f,&a,&b,&c); if (a + b = c | a + b = b | b + c = a | a = 0 | b = 0 | c = 0)/*满足三角形边长条件*/ printf(Data is errorn); else s = 0.5 * (a + b + c); area = sqrt(s * (s - a) * (s - b) * (s - c); printf(Area is %.2fn,area); 运行结果:Please input:4 5 7Area is 9.806#include voi
8、d main() int a , b , c; printf (Please input 3 numbersn); scanf (%d %d %d,&a,&b,&c); if (a b) if (b c) printf (Max = %dn,c); else printf (Max = %dn,b); else if (a c) printf (Max = %dn,c); else printf (Max = %dn,a);运行结果:Please input 3 numbers73 -54 82Max = 827.#include #include void main() float a ,
9、b , c , delta; double x; printf (Please input:n); scanf (%f %f %f, &a , &b , &c); delta = b * b - 4 * a * c;/*进行delta判断*/ if ( delta 0 ) printf (No real rootn); else if ( delta = 0 ) printf (Only one root = %fn, -b / (2 * a) );else x = -b + sqrt(delta) / (2 *a); printf (One root is : %fn, x); x = -b
10、 - sqrt(delta) / (2 *a); printf (The other root is : %fn, x); 运行结果:Please input:1 -1 -30One root is : 6.500000The other root is : -4.5000008.#include void main() float x , y; printf (请输入xn); scanf (%f,&x); if (x = 1 & x 3) y = 2 * x - 1; else y = 3 * x * x - 10; printf (y = %.2fn,y);运行结果:请输入x2.5y =
11、4.0010.#include void main() int value1 , value2; float value3; char operate; printf (Input your exprssion:n); scanf (%d%c%d,&value1,&operate,&value2); switch (operate)case+:printf(%d%c%d=%dn,value1,operate,value2,value1 +value2);break; case-:printf(%d%c%d= %dn,value1,operate,value2,value1 - value2);
12、 break; case*:printf(%d%c%d= %dn,value1,operate,value2,value1 * value2); break; case /: if (value2 = 0) printf (Divishion by zero.n); else value3 = (float) value1 /value2;printf(%d%c%d= %.2fn,value1,operate,value2,value3); break; default: printf (Unknown operate.n); 运行结果:Input your exprssion9/59/5 =
13、 1.809.#include void main() int score; printf (请输入成绩:); scanf (%d,&score); if (score 100) | (score 0) printf (Input errorn); else score = score / 10; switch (score) case 10:printf(An);break; case 9:printf(An);break; case 8:printf(Bn);break; case 7:printf(Cn);break; case 6:printf(Dn);break; default:p
14、rintf(En);break; 运行结果:请输入成绩:89B第五章1满足条件与不满足条件,与分支表达式完全相同。都是对表达式值进行非零判断。2循环变量是指在循环程序中随着循环的进行而改变的变量,例如i+中的i是常见的循环变量。当循环中出现break语句是有可能结束整个循环,而当循环中出现continue时,有可能结束一次循环。3while与do-while都是循环语句,while执行是先判断是否满足括号内的条件,而do-while先进行一次do的运算,再判断。一般情况下,两语句处理同一问题时若二者的循环题部分是一样的,它们的结果也一样。但是如果while后面的表达式一开始就为假,两种循环的结
15、果是不同的:while语句不会执行,而do-while语句会执行一次。4continue语句只是结束本次循环,而不是终止整个循环的执行。而break语句则是结束整个循环过程,不再判断执行循环的条件是否成立。5(1)#include void main() int sum = 0 , i = 1;while(sum1000) sum += i; i+; printf(i=%dn,i);运行结果:i=46(2)#includevoid main() float sum = 0; int n = 1; while(sum 2) sum += (float)1 / (n +); printf(n =
16、%dn,n);运行结果:n = 56#include void main() int a , b , x , y , u; printf(Please input 2 numbers:n); scanf(%d %d,&x,&y); for(k=0;k=4-2*i;k+) printf(*); printf(n); 运行结果: * * * * * *8略9#includevoid main()char i,j,k; /*i是a的对手;j是b的对手;k是c的对手*/ for(i = x;i = z;i +) for(j = x;j = z;j +)if (i != j) for(k = x;k y
17、) u = x; x = y; y = u; /* 保证x y */ a = x , b = y; while (b != 0) u = a % b; a = b; b = u; /* 辗转相除法求最大公约数 */ printf(最大公约:%dn最小公倍:%dn,a, x * y / a); 运行结果:Please input 2 numbers:64 80最大公约:16最小公倍:3207#include stdio.hvoid main() int i,k,j; /*根据星星排列的规律找出公式*/ for (i=0;i=3;i+) for (j=0;j=2-i;j+) printf( );
18、/*根据行数算出空格数*/ for (k=0;k=2*i;k+) printf(*); printf(n); /*根据行数算出星星数*/ for (i=0;i=2;i+) for (j=0;j=i;j+) printf( ); 运行结果:The order is:a - zb - xc - y10#includevoid main() int x , y , z , s1 , s2;/*此题用的是穷举法,就是每个数都去试一遍,直到得出结果*/ for(x = 0;x =40;x +) for(y = x;y = 40;y +) for(z = y;z = 40;z +) s1 = x + y
19、+ z; s2 = x * x + y * y + z * z; if (s1 = 40 & s2 = 600) printf(方程解为:x=%d y=%d z=%dn,x,y,z); 运行结果:方程的解为: x=10 y=10 z=2011#include #include int main()float a; /* 被开方数 */float x0, x1; /* 分别代表前一项和后一项 */ printf(Input a positive number:n); scanf(%f, &a); x0 = a / 2; /* 任取初值 */ x1 = (x0 + a / x0) / 2; whi
20、le (fabs(x1 - x0) = 1e-5) x0 = x1; x1 = (x0 + a / x0) / 2; printf(The square root of %5.2f is %8.5fn, a, x1); return 0;运行结果:Input a positive number:2The square root of 2.00 is 1.4142112.#include int cow(int x); void main(void) int i=1; int sum=1; for(i;i=20;i+) sum+=cow(i); printf(有%d头牛n,sum); int c
21、ow(int n) int m=n-4; int sum=0; if(n=1;m-) sum+=cow(m); return sum; 运行结果:有658头牛3#include void main() int a44,i,j,s1; printf(输入矩阵n); for (i=0;i=3;i+) for (j=0;j=3;j+) scanf(%d,&aij); /*搜索矩阵对角线*/ for (i=0,s1=0;i=3;i+) s1+=aii; s1+=a3-ii; printf(对角元素之和是%dn,s1); 运算结果:输入矩阵22 37 45 9844 75 96 1222 37 55 8
22、679 46 38 15对角线元素之和是4774#include void main() int a10,b9,i;printf(请输入a数组的内容); for (i=0;i=9;i+) scanf(%d,&ai); /*顺次计算b数组的内容*/for (i=1;i=9;i+) bi-1=ai/ai-1; for (i=1;i=9;i+) printf(%d ,bi-1); if (i%3=0) printf(n); 第六章1(1)、(2)均为错错在:C语言不允许定义动态数组,即数组的长度不能依赖于程序运行过程中变化着的量。(3)错错在地址符的输入错误,应将&a改为&ai 其中i为小于5的数(
23、4)对2#include void main() int i,j,p,a10; printf(输入十个整数); for (i=0;i10;i+) scanf(%d,&ai); /*用冒泡排序算法*/ for (j=0;j=8;j+) for (i=0;iai+1) /*大的就沉下去,即交换*/ p=ai; ai=ai+1; ai+1=p; printf(排序之后:n); for (i=0;i=9;i+) printf(%d ,ai);运算结果:输入十个整数5 7 8 9 6 3 4 7 2 1排序之后:1 2 3 4 5 6 7 7 8 9 6#include void main() char str100; int n; printf(请输入字符串); scanf(%s,&str); n=0;while (strn!=0) n+; n-;/*减掉最后的一位*/0 printf(倒序输出的是:); while (n=0) printf(%c,strn); n-; 运算结果:请输入字符串:abcdefg倒序输出的是:gfedcba7#include void main() int i=0,j=0; char a100,b100; printf(请输入字符串a,bn); scanf(%s,&a); scanf(%s,&b); /*求出a字符串的长度*/ while (ai!=0)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1