C程序的设计课后复习题答案46章.docx
- 文档编号:25020456
- 上传时间:2023-06-03
- 格式:DOCX
- 页数:19
- 大小:16.78KB
C程序的设计课后复习题答案46章.docx
《C程序的设计课后复习题答案46章.docx》由会员分享,可在线阅读,更多相关《C程序的设计课后复习题答案46章.docx(19页珍藏版)》请在冰豆网上搜索。
C程序的设计课后复习题答案46章
《C程序设计》课后习题答案(4-6章)
第四章
8、
#definePI3.1415926
#include
voidmain()
{
floath,r,l,s,sq,vq,vz;
printf("pleaseinputr,h:
");
scanf("%f,%f",&r,&h);
l=2*PI*r;
s=r*r*PI;
sq=4*PI*r*r;
vq=4.0/3.0*PI*r*r*r;
vz=PI*r*r*h;
printf("l=%6.2f\n",l);
printf("s=%6.2f\n",s);
printf("sq=%6.2f\n",sq);
printf("vq=%6.2f\n",vq);
printf("vz=%6.2f\n",vz);
}
9、
#include
voidmain()
{
floatc,f;
printf("pleaseinputf:
");
scanf("%f",&f);
c=(5.0/9.0)*(f-32);
printf("c:
%6.2f\n",c);
}
第五章
5、
#include
voidmain()
{
intx,y;
printf("pleaseinputx:
");
scanf("%d",&x);
if(x<1)
{y=x;
printf("x=%d,y=x=%d\n",x,y);}
elseif(x<10)
{y=2*x-1;
printf("x=%d,y=2*x-1=%d\n",x,y);}
else
{y=3*x-11;
printf("x=%d,y=3*x-11=%d\n",x,y);}
}
6、
#include
voidmain()
{
floatscore;
chargrade;
printf("pleaseinputthestudentscore:
");
scanf("%f",&score);
while((score>100)||(score<0))
{printf("error,pleaseinputagain!
\n");
printf("pleaseinputthestudentscore:
");
scanf("%f",&score);
}
switch((int)(score/10))
{
case10:
case9:
grade='A';break;
case8:
grade='B';break;
case7:
grade='C';break;
case6:
grade='D';break;
case5:
case4:
case3:
case2:
case1:
case0:
grade='E';
}
printf("scoreis%f,gradeis%c\n",score,grade);
}
7、
#include
#include
voidmain()
{
longintnum;
intindiv,ten,hundred,thousand,ten_thousand,place;
printf("pleaseinputaninteger(0-99999):
");
scanf("%ld",&num);
if(num>9999)place=5;
elseif(num>999)place=4;
elseif(num>99)place=3;
elseif(num>9)place=2;
elseplace=1;
printf("Totaldigits:
%d\n",place);
printf("Foreachnumber:
");
ten_thousand=num/10000;
thousand=(int)(num-ten_thousand*10000)/1000;
hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;
ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);
if(ten_thousand!
=0)printf("%d,%d,%d,%d,%d",ten_thousand,thousand,hundred,ten,indiv);
elseif(thousand!
=0)printf("%d,%d,%d,%d",thousand,hundred,ten,indiv);
elseif(hundred!
=0)printf("%d,%d,%d",hundred,ten,indiv);
elseif(ten!
=0)printf("%d,%d",ten,indiv);
elseprintf("%d",indiv);
printf("\nThereversenumber:
");
switch(place)
{case5:
printf("%d%d%d%d%d",indiv,ten,hundred,thousand,ten_thousand);
break;
case4:
printf("%d%d%d%d",indiv,ten,hundred,thousand);
break;
case3:
printf("%d%d%d",indiv,ten,hundred);
break;
case2:
printf("%d%d",indiv,ten);
break;
case1:
printf("%d",indiv);
break;
}
}
8、
(1)
#include
voidmain()
{
longi;
floatbonus,bon1,bon2,bon3,bon4,bon6,bon10;
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("pleaseinputbonus:
");
scanf("%ld",&i);
if(i<=100000)bonus=i*0.1;
elseif(i<=200000)bonus=bon1+(i-100000)*0.075;
elseif(i<=400000)bonus=bon2+(i-200000)*0.05;
elseif(i<=600000)bonus=bon4+(i-400000)*0.03;
elseif(i<=1000000)bonus=bon6+(i-600000)*0.015;
elsebonus=bon10+(i-1000000)*0.01;
printf("bonus:
%10.2f\n",bonus);
}
(2)
#include
voidmain()
{
longi;
floatbonus,bon1,bon2,bon4,bon6,bon10;
intbranch;
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("pleaseinputbonus:
");
scanf("%ld",&i);
branch=i/100000;
if(branch>10)branch=10;
switch(branch)
{
case0:
bonus=i*0.1;
break;
case1:
bonus=bon1+(i-100000)*0.075;
break;
case2:
case3:
bonus=bon2+(i-200000)*0.05;
break;
case4:
case5:
bonus=bon4+(i-400000)*0.03;
break;
case6:
case7:
case8:
case9:
bonus=bon6+(i-600000)*0.015;
break;
case10:
bonus=bon10+(i-1000000)*0.01;
}
printf("bonusis%f\n",bonus);
}
9、
#include
voidmain()
{
inta,b,c,d,t;
printf("pleaseinputfournumber:
");
scanf("%d,%d,%d,%d",&a,&b,&c,&d);
printf("a=%d,b=%d,c=%d,d=%d\n",a,b,c,d);
if(a>b)
{t=a;a=b;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);
}
10、
#include
#include
voidmain()
{
inth=10;
floatx1=2,y1=2,x2=-2,y2=2,x3=-2,y3=-2,x4=2,y4=-2,x,y,d1,d2,d3,d4;
printf("pleaseinput(x,y):
");
scanf("%f,%f",&x,&y);
d1=sqrt((x-x4)*(x-x4)+(y-y4)*(y-y4));
d2=sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1));
d3=sqrt((x-x2)*(x-x2)+(y-y2)*(y-y2));
d4=sqrt((x-x3)*(x-x3)+(y-y3)*(y-y3));
if(d1>1&&d2>1&&d3>1&&d4>1)h=0;
printf("height%d\n",h);
}
第六章
1、
#include
voidmain()
{
intp,r,n,m,temp;
printf("pleaseinputtwointeger:
");
scanf("%d,%d",&n,&m);
if(n {temp=n; n=m; m=temp; } p=n*m; r=n%m; while(r! =0) { n=m; m=r; r=n%m; } printf("maxgongyueshu: %d\n",m); printf("mingongbeishu: %d\n",p/m); } 2、 #include voidmain() { charc; intletters=0,space=0,digit=0,other=0; printf("pleaseinputstring: \n"); while((c=getchar())! ='\n') { if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) letters++; elseif(c=='') space++; elseif(c>='0'&&c<='9') digit++; elseother++; } printf("letters: %d\nspace: %d\ndigit: %d\nother: %d\n",letters,space,digit,other); } 3、 #include voidmain() { inta,n; inti=1,sn=0,tn=0; printf("pleaseinputa,n: "); scanf("%d,%d",&a,&n); while(i<=n) {tn=tn+a; sn=sn+tn; a=a*10; ++i; } printf("a+aa+aaa+^^^^^=%d\n",sn); } 4、 #include voidmain() { floats=0,t=1; intn; for(n=1;n<=20;n++) { t=t*n; s=s+t; } printf("1! +2! +^^^^^^^^+20! =%e\n",s); } 5、 #include voidmain() { intn1=100,n2=50,n3=10; floatk; floats1=0,s2=0,s3=0; for(k=1;k<=n1;k++) s1=s1+k; for(k=1;k<=n2;k++) s2=s2+k*k; for(k=1;k<=n3;k++) s3=s3+1/k; printf("sum=%f\n",s1+s2+s3); } 6、 #include voidmain() { inti,j,k,n; printf("\nnarcissusnumberare"); for(n=100;n<1000;n++) { i=n/100; j=(n-100*i)/10; k=n-100*i-10*j; if(n==i*i*i+j*j*j+k*k*k) printf("%d",n); } } 7、 #include voidmain() { intm,s,i; for(m=2;m<1000;m++) {s=0; for(i=1;i if(m%i==0)s=s+i; if(s==m) {printf("%d,itsfactorsare",m); for(i=1;i if(m%i==0)printf("%d",i); printf("\n"); } } } 8、 #include voidmain() { inti,t,n=20; floata=2,b=1,s=0; for(i=1;i<=n;i++) {s=s+a/b; t=a; a=a+b; b=t; } printf("sum=%f\n",s); } 9、 #include voidmain() { floatsn=100,hn=sn/2; intn; for(n=2;n<=10;n++) { sn=sn+2*hn; hn=hn/2; } printf("tentharriveat%fmeter.\n",sn); printf("tenth%fmeter.\n",hn); } 10、 #include voidmain() { intday,x1,x2; day=9; x2=1; while(day>0) { x1=(x2+1)*2; x2=x1; day--; } printf("total=%d\n",x1); } 11、 #include #include voidmain() {floata,x0,x1; printf("Enterapositivenumber: "); scanf("%f",&a); x0=a/2; x1=(x0+a/x0)/2; do { x0=x1; x1=(x0+a/x0)/2; }while(fabs(x0-x1)>=1e-5); printf("Thesquarerootof%fis%f\n",a,x1); } 12、 #include #include voidmain() { floatx1,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("Therootofequationis%f\n",x1); } 13、 #include #include voidmain() { floatx0,x1,x2,fx0,fx1,fx2; do {printf("Enterx1andx2: "); scanf("%f,%f",&x1,&x2); fx1=x1*((2*x1-4)*x1+3)-6; fx2=x2*((2*x2-4)*x2+3)-6; }while(fx1*fx2>0); do {x0=(x1+x2)/2; fx0=x0*((2*x0-4)*x0+3)-6; if((fx0*fx1)<0) {x2=x0; fx2=fx0; } else {x1=x0; fx1=fx0; } }while(fabs(fx0)>=1e-5); printf("x=%f\n",x0); } 14、 #include voidmain() { inti,j,k; for(i=0;i<=3;i++) {for(j=0;j<=2-i;j++) printf(""); for(k=0;k<=2*i;k++) printf("*"); printf("\n"); } for(i=0;i<=2;i++) {for(j=0;j<=i;j++) printf(""); for(k=0;k<=4-2*i;k++) printf("*"); printf("\n"); } } 15、 #include voidmain() { chari,j,k;/*i是A的对手;j是B的对手;k是C的对手*/ for(i='X';i<='Z';i++) for(j='X';j<='Z';j++) for(k='X';k<'Z';k++) if(i! =j&&i! =k&&j! =k) if(i! ='X'&&k! ='X'&&k! ='Z') printf("A--%c\tB--%c\tC--%c\n",i,j,k); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序 设计 课后 复习题 答案 46