当前位置:文档之家› 最新c语言课后习题答案汇总

最新c语言课后习题答案汇总

c语言课后习题答案第二章习题2.什么叫做结构化算法?为什么要提倡结构化算法?答:结构化算法是由一些基本结构顺序组成的。

在基本结构之间不存在向前或向后的跳转,流程的转移只存在于一个基本的结构范围内。

一个非结构化的算法可以用一个等价的结构化算法代替,其功能不变。

跟结构化算法比较起来,非结构化算法有以下缺点:流程不受限制的随意转来转去,使流程图豪无规律使人在阅读的时候难以理解算法的逻辑.难以阅读,也难以修改,从而使算法的可靠性和可维护性难以保证。

4.第三章习题1.#include<stdio.h>#include<math.h>void main(){unsigned int n;float p,p1,r=0.09;scanf("%u",&n);p=pow(1+r,n);p1=(p-1)*100;printf("%5.2f%%\n",p1);}运行结果:输入,回车,见结果:2.#include<stdio.h>#include<math.h>int main(){int bj=1000;float r1,r2,r3,r5,r0,lx1,lx2,lx3,lx4,lx5;r1=0.0414;r2=0.0468;r3=0.0540;r5=0.0585;r0=0.0072;lx1=bj*r5;lx2=bj*(1+r2)*r3;lx3=bj*(1+r3)*r2;lx4=bj*pow(1+r1,5);lx5=bj*r0*5;printf("lx1=%f lx2=%f lx3=%f lx4=%f lx=5%f\n",lx1,lx2,lx3,lx4,lx5); return 0;}运行结果:3.#include<stdio.h>#include<math.h>int main(){long d,p;d=300000;p=6000;double m,r=0.01;m=log(p/(p-d*r))/log(1+r);printf("%4.2f",m);return 0;}运行结果:4.#include<stdio.h>int main(){int c1,c2;c1=97;c2=98;printf("c1=%c,c2=%c\n",c1,c2);printf("c1=%d,c2=%d\n",c1,c2);return 0;}运行结果:①;②;③结果不变。

5.#include<stdio.h>int main(){int a,b;float x,y;char c1,c2;scanf("a=%db=%d",&a,&b);scanf("%f%e",&x,&y);scanf("%c%c",&c1,&c2);return 0;}运行结果:输入即可。

6.#include<stdio.h>int main(){char c1,c2,c3,c4,c5;c1='C';c2='h';c3='i';c4='n';c5='a';c1=c1+4;c2=c2+4;c3=c3+4;c4=c4+4;c5=c5+4;printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5);return 0;}运行结果:。

7.#include<stdio.h>#include<math.h>int main(){float r,h,pi,c,s,sb,v,vz;pi=3.141592653;scanf("%4f%f",&r,&h);c=2*pi*r;s=pi*pow(r,2);sb=4*pi*pow(r,2);v=4/3*pi*pow(r,3);vz=s*h;printf("c=%7.2f,s=%7.2f,sb=%7.2f,v=%7.2f,vz=%7.2f\n",c,s,sb,v,vz); return 0;}运行结果:输入,得结果。

第四章习题4. #include<stdio.h>int main(){printf("please input three numbers !\n");long a,b,c,max;scanf("%ld,%ld,%ld",&a,&b,&c);if(a>b)if(a>c)max=a;elsemax=c;if(a>c)max=b;else if(b>c)max=b;elsemax=c;printf("max=%ld\n",max);return 0;}运行结果:5.#include<stdio.h>#include<math.h>void main(){double p,x;printf(" Please enter a less than 1000 positive Numbers!\n"); scanf("%lf",&x);if(x<1000)p=sqrt(x);elseprintf("Input error, please enter again!\n");printf("%5.0lf\n",p);}运行结果:6.#include<stdio.h>int main(){double x,y;scanf("%lf",&x);if(x<1)else if(x>=1&&x<10)y=2*x-1;elsey=3*x-11;printf("y=%5.2f\n",y);return 0;}运行结果:8. #include<stdio.h>int main(){float score;printf("your score:");scanf("%f",&score);if(score>90)printf("A\n");else if(score>80&&score<89)printf("B\n");else if(score>70&&score<79)printf("C\n");else if(score>60&&score<69)printf("D\n");elseprintf("E\n");return 0;}运行结果:9#include<stdio.h>int main(){int x;int x1,x2,x3,x4,x5;printf("请任意输入一个小于99999的数:\n"); scanf("%d",&x);x1=x/10000;x2=(x-x1*10000)/1000;x3=(x-x1*10000-x2*1000)/100;x4=(x-x1*10000-x2*1000-x3*100)/10;x5=(x-x1*10000-x2*1000-x3*100-x4*10)%10;if(x1>=1){ printf("输入的数是5位数。

\n");printf("按位从高位到低位输出结果是:\n");printf("%d,%d,%d,%d,%d\n",x1,x2,x3,x4,x5);printf("逆序输出结果是:\n");printf("%d%d%d%d%d",x5,x4,x3,x2,x1);}else if(x2>=1){ printf("输入的数是4位数\n");printf("按位从高位到低位输出结果是:\n");printf("%d,%d,%d,%d\n",x2,x3,x4,x5);printf("逆序输出结果是:\n");printf("%d%d%d%d",x5,x4,x3,x2);}else if(x3>=1){ printf("输入的数是3位数\n");printf("按位从高位到低位输出结果是:\n");printf("%d,%d,%d\n",x3,x4,x5);printf("逆序输出结果是:\n");printf("%d%d%d",x5,x4,x3);}else if(x4>=1){ printf("输入的数是2位数\n");printf("按位从高位到低位输出结果是:\n");printf("%d,%d\n",x4,x5);printf("逆序输出结果是:\n");printf("%d%d",x5,x4);}else{printf("输入的数是1位数\n");printf("按位从高位到低位输出结果是:\n");printf("%d\n",x5);printf("逆序输出结果是:\n");printf("%d",x5);}printf("\n");return 0;}10. 方法一:使用if语句实现#include <stdio.h>#include<conio.h>int main(){long int i;float tc,jj1,jj2,jj4,jj6,jj10;jj1=100000*0.1; /*利润为10万元时的奖金*/jj2=jj1+100000*0.075; /*利润为20万元时的奖金*/jj4=jj2+200000*0.05; /*利润为40万元时的奖金*/jj6=jj4+200000*0.03; /*利润为60万元时的奖金*/jj10=jj6+400000*0.015; /*利润为100万元时的奖金*/printf("请输入利润i:");scanf("%ld",&i);if(i<=100000)tc=i*0.1; /*利润在10万元以内按0.1提成奖金*/ else if(i<=200000)tc=jj1+(i-100000)*0.075; /*利润在10万至20万元时的奖金*/ else if(i<=400000)tc=jj2+(i-200000)*0.05; /*利润在20万至40万元时的奖金*/ else if(i<=600000)tc=jj4+(i-400000)*0.03; /*利润在40万至60万元时的奖金*/ else if(i<=1000000)tc=jj6+(i-600000)*0.015; /*利润在60万至100万元时的奖金*/ elsetc=jj10+(i-1000000)*0.01; /*利润在100万元以上时的奖金*/ printf("奖金是%10.2f\n",tc);getch();return 0;}方法二:使用switch语句实现#include <stdio.h>void main(){ long i;float bonus, bon1, bon2, bon4, bon6, bon10;int c;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;printf("请输入利润i:");scanf("%ld",&i);c=i/100000;if(c>10) c=10;switch(c){ case 0: bonus=1*0.1;break;case 1: bonus=bon1+(i-100000)*0.075;break; case 2 :case 3: bonus=bon2+(i-200000)*0.05; break; case 4:case 5: bonus=bon4+(i-400000)*0.03;break; case 6:case 7:case 8:case 9: bonus=bon6+(i-600000)*0.015;break; case 10: bonus=bon10+(i-1000000)*0.01;}printf("奖金是%10.2f\n",bonus);}⑾#include<stdio.h>int main(){int a,b,c,d,t;printf("please enter four integer:\n "); scanf("%d %d %d %d",&a,&b,&c,&d);if(a>b){t=a;b=t;}if(a>c){t=a;a=c;c=t;}if(a>d){t=a;a=d;d=t;}if(b>c){t=b;b=c;c=t;}if(b>d){t=b;b=d;d=t;}if(c>d){t=c;c=d;d=t;}printf("%d,%d,%d,%d\n",a,b,c,d); return 0;}运行结果:⑿#include<stdio.h>int main(){float x1,x2,x3,x4,y1,y2,y3,y4,x,y; x1=2;y1=2;x2=-2;y2=2;x3=-2;x4=2;y4=-2;printf("Please input coordinates:\n ");scanf("%f,%f",&x,&y);if((x-x1)*(x-x1)+(y-y1)*(y-y1)>1&&(x-x2)*(x-x2)+(y-y2)*(y-y2)>1&&(x-x3)*(x-x3)+(y-y3)*(y-y3)>1&&(x-x4)*(x-x4)+(y-y4)*(y-y4)>1)printf("The height is 0!\n");elseprintf("The height of the tower is 10 meters!\n");return 0;}运行结果:第五章习题3.#include<stdio.h>int main(){unsigned int m,n,y,b,t,i,j;printf("please input two positive integers:\n");scanf("%u,%u",&m,&n);if(m<n){t=m;m=n;n=t;}for(i=1;i<=n;i++){if(n%i==0&&m%i==0)y=i;}for(j=m;;j++){if(j%m==0&&j%n==0)break;}b=j;printf("Maximum when=%u,Minimum LCD=%u\n",y,b);return 0;}运行结果:4.#include<stdio.h>int main(){char c;int m,n,p,q;m=0;n=0;p=0;q=0;while((c=getchar())!=EOF){if((c>='A'&&c<='Z')||(c>='a'&&c<='z'))m++;else if(c==32)n++;else if(c>=48&&c<=57)p++;elseq++;}printf("English letters=%d,Spaces=%d,digital=%d,Other characters=%d",m,n,p,q);return 0;}运行结果:5.#include<stdio.h>int main(){unsigned a,n;long s=2;int i;scanf("%u,%u",&a,&n);for(i=1;i<n;i++){a=a*10+2;s=s+a;}printf("%ld\n",s);return 0;}运行结果:6.#include<stdio.h>int main(){int i,j,s,s1;s=0;for(i=1;i<=20;i++){s1=1;{for(j=i;j>0;j--)s1=s1*j;}s=s+s1;}printf("%d\n",s);.return 0;}运行结果:7.#include<stdio.h>int main(){double s1,s2,s3,s,i,a,b; s1=0;s2=0;s3=0;for(i=1;i<=100;i++)s1=s1+i;for(i=1;i<=50;i++){a=i*i;s2=s2+a;}for(i=1;i<=10;i++){b=1.0/i;s3=s3+b;}s=s1+s2+s3;printf("The results=%f\n",s); return 0;}运行结果:8.#include<stdio.h>int main(){int a,b,c,i;for(i=100;i<1000;i++){a=i/100;b=i/10-a*10;c=i%10;if(a*a*a+b*b*b+c*c*c==i)printf("\n%d",i); }printf("\n");return 0;}运行结果:9.#include<stdio.h>int main(){int s,i,j;for(i=6;i<1000;i++){s=0;for(j=1;j<=i/2;j++){if(i%j==0)s=s+j;}if(s==i){printf("%d its factors are : ",i);for(j=1;j<=i/2;j++)if(i%j==0)printf("%d,",j);printf("\n");}}return 0;}运行结果:⑽#include<stdio.h>int main(){double a1,a2,an,s,t;int i;a1=1;a2=2;s=0;for(i=1;i<=20;i++){an=a2/a1;s=s+an;t=a2;a2=a1+a2;a1=t;}printf("2/1+3/2+5/3+...+=%f\n",s);return 0;}运行结果:⑾#include<stdio.h>int main(){double h,s,t;int i;h=100;s=0;for(i=1;i<=10;i++){h=h*0.5;}printf("The height of the tenth rebound=%1.8f\n",h);t=h;h=100;for(i=1;i<10;i++){s=s+h+h*0.5;h=h*0.5;}printf("The 10th fall in total journey=%6.8f\n",s+t);return 0;}运行结果:⑿#include<stdio.h>int main(){int i,x=1;for(i=1;i<10;i++)x=2*(x+1);printf("The first day of the total number of peach vintage=%d\n",x); return 0;}运行结果:⒀#include<stdio.h>#include<math.h>void main(){int a = 3;double esp ;double x, temp;scanf("%d", &a);x = 1.0;do{temp = x;x = (1.0 / 2.0)* (x + a / x);esp = fabs(x - temp);}while( esp >= 1e-5);printf("%f\n ", x);}运行结果:⒁#include<stdio.h>#include <math.h>void main() { float x1,x0,f,f1;x1=1.5;do{ x0=x1;f=((2*x0-4)*x0+3)*x0-6;f1=(6*x0-8)*x0+3;x1=x0-f/f1;}while(fabs(x1-x0)>=1e-5);printf("The root of equation is %5.2f\n",x1);}运行结果:⒂第六章习题1.#include<stdio.h>void main(){int a[100];int i,j;for(i=1;i<100;i++){a[i]=i;for(j=2;j<=i;j++)if(j<i)if(a[i]%j==0)break;if(a[i]==j)printf("%3d,",a[i]);}}}运行结果:2. #include<stdio.h>void main(){int i,j,t;int a[10];printf("Input ten integer:\n");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;}printf("According to the order for since the output:\n");for(i=0;i<10;i++)printf("%d,",a[i]);}运行结果:3.#include<stdio.h>int main(){int i,j;int a[3][3];printf("Input array of data:\n");for(i=0;i<3;i++)for(j=0;j<3;j++)scanf("%d,",&a[i][j]);for(i=0;i<3;i++)for(j=0;j<3;j++)if(i==j)s=s+a[i][j];printf("Diagonal elements combined:\n");printf("%d\n",s);return 0;}运行结果:4.#include<stdio.h>void main(){static int a[10]={1,7,8,17,23,24,59,62,101}; int i,t;scanf("%d",&a[9]);for(i=9;i>0;i--)if(a[i]<a[i-1]){t=a[i-1];a[i-1]=a[i];a[i]=t;}for(i=0;i<10;i++)printf("%d,",a[i]);printf("\n");}运行结果:5.#include<stdio.h>void main(){int i,j,t;int a[10];printf("Input ten integer:\n");for(i=0;i<10;i++)scanf("%d,",&a[i]);for(i=0;i<9;i++)for(j=0;j<9-i;j++){if(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}printf("According to the order of childhood output:\n");for(j=0;j<10;j++)printf("%d,",a[j]);printf("\n");printf("Press from big to small order output:\n");for(j=9;j>=0;j--)printf("%d,",a[j]);}运行结果:6.#include<stdio.h>#define N 10void main(){int i,j;int a[N][N];for(i=0;i<N;i++){a[i][0]=1;a[i][i]=1;}for(i=2;i<N;i++)for(j=1;j<=i-1;j++){a[i][j]=a[i-1][j-1]+a[i-1][j];}for(i=0;i<N;i++){for(j=0;j<=i;j++)printf("%6d",a[i][j]);printf("\n");}printf("\n");}运行结果:7.8.9.10.#include<stdio.h>#include<string.h>void main(){char s[3][80];int m=0,n=0,p=0,q=0,r=0,i,j;gets(s);for(i=0;i<3;i++)for(j=0;j<80;j++){if(s[i][j]>='A'&&s[i][j]<='Z')m++;else if(s[i][j]>='a'&&s[i][j]<='z')n++;else if(s[i][j]>='0'&&s[i][j]<='9')p++;else if(s[i][j]==32)q++;elser++;}printf("%d,%d,%d,%d,%d\n",m,n,p,q,r); }运行结果:11.12.#include<stdio.h>#include<string.h>void main(){int i;char c1[80],c2[80];gets(c1);for(i=0;i<80;i++){if(c1[i]>='A'&&c1[i]<='Z'){c2[i]=155-c1[i];}else if(c1[i]>='a'&&c1[i]<='z'){c2[i]=219-c1[i];}elsec2[i]=c1[i];}printf("%s\n%s\n",c1,c2);}运行结果:13.#include<stdio.h>#include<string.h>void main(){char s1[10],s2[10],s3[21];int i,j;gets(s1);gets(s2);for(i=0;s1[i]!='\0';i++)s3[i]=s1[i];for(j=0;s2[j]!='\0';j++)s3[j+i]=s2[j];s3[j+i]='\0';printf("%s\n%s\n%s\n",s1,s2,s3);}运行结果:14.#include<stdio.h>#include<string.h>void main(){char s1[10],s2[10];int i,a;gets(s1);gets(s2);for(i=0;s1[i]!='\0'&&s2[i]!='\0';i++) {if(s1[i]==s2[i])a=0;elsea=s1[i]-s2[i];break;}printf("%d\n",a);}运行结果:15.。

相关主题