资源描述:
《C语言书后答案》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、习题21.C2.A3.①2.0②24.x=6,y=7.z=-85.#definePI3.1415926#includevoidmain(){doubler,S,V;printf("请输入球体半径:");scanf("%lf",&r);S=4*PI*r*r;V=4./3*PI*r*r*r;printf("r=%10.2lf,S=%10.2lf,V=%10.2lf",r,S,V);}6.#include#includevoidmain(){doublex1,y1,
2、x2,y2,d;printf("请输入两点坐标x1,y1,x2,y2:");scanf("%lf,%lf,%lf,%lf",&x1,&y1,&x2,&y2);d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));printf("d=%10.2lf",d);}7.#includevoidmain(){inta,x,y,z;printf("请输入一个三位数的正整数:");scanf("%i",&a);z=a%10;x=a/100;y=(a-x*100)/10;printf("
3、百位数=%d,十位数=%d,个位数=%d",x,y,z);}8.#includevoidmain(){chara,b,c;printf("请输入两个字符:");scanf("%c,%c",&a,&b);c=a;a=b;b=c;printf("a=%c,b=%c",a,b);}习题四:基础部分:1.#includevoidmain(){inti,sum=0;for(i=2;i<=100;i+=2){sum=sum+i;}printf("sum=%d",sum);}2.#includ
4、evoidmain(){inti,n;doublesum=0;printf("请输入正整数n:");scanf("%d",&n);for(i=1;i<=n;i++){sum=sum+1.0/i;}printf("sum=%lf",sum);}3.#includevoidmain(){inti,n;doublePI=1;printf("请输入正整数n:");scanf("%d",&n);for(i=1;i<=n;i++){PI=PI*2.0*i*2*i/(2*i-1)/(2*i+1
5、);}PI=2*PI;printf("PI=%lf",PI);}4.#includevoidmain(){inta,n,i,sum=0;printf("请输入正整数a,n:");scanf("%d%d",&a,&n);for(i=1;i<=n;i++){sum=sum+a;a=a*10+a;}printf("%d",sum);}5.简便做法:#includevoidmain(){intx,y,z;for(z=1;z<11;z++)for(y=1;y<25;y++){x=100
6、-9*z-y;if(x>0&&2*x+4*y+2*z==100)printf("鸡为%d只,狗为%d只,几头鸟为%d只。",x,y,z);}}常规做法:#includevoidmain(){intx,y,z;for(x=1;x<99;x++)for(y=1;y<99;y++)for(z=1;z<99;z++)if(2*x+4*y+2*z==100&&x+y+9*z==100)printf("%d%d%d",x,y,z);}6.#include#include7、>voidmain(){intm,i,k;for(m=100;m<=200;m++){k=sqrt(m);for(i=2;i<=k;i++)if(m%i==0)break;if(i>=k+1)printf("%dt",m);}}7.#includevoidmain(){charx;while((x=getchar())!=''){if(x<48
8、
9、x>57)printf("%c",x);}}8.#includevoidmain(){inta,b,c,x;for(x=100;
10、x<=999;x++){a=x%10;b=(x/10)%10;c=x/100;if(x==a*a*a+b*b*b+c*c*c)printf("%dt",x);}}9.x>0x#includevoidmain(){inta,b,c,d,x;for(x=1000;x<10000;x++){if(