C語(yǔ)言程序設(shè)計(jì)(科學(xué)出版社)第4章 課后習(xí)題參考答案
C語(yǔ)言程序設(shè)計(jì)習(xí)題參考答案第4章 習(xí)題參考答案1 判斷題123456×××2 選擇題12345678BBBBDABB3 程序閱讀題(1) 105(2) a=8(3) abc123DEF(4)4 程序填空題(1) chch=')'count-count=0count>0(2) div=ndiv(3) min=mark min=mark max=mark sum+=mark5 編程題(1) #include <stdio.h>void main( ) int x,y; printf("Enter the X,Y=?n"); scanf("%d,%d",&x,&y); if(x>0 && y>0) printf("(%d,%d)是第一像限n",x,y); else if(x>0 && y<0) printf("(%d,%d)是第四像限n",x,y); else if(x<0 && y>0) printf("(%d,%d)是第三像限n",x,y); else if(x<0 && y<0) printf("(%d,%d)是第二像限n",x,y); else if(x>0 && y=0) printf("(%d,%d)在X軸正方向n",x,y); else if(x<0 && y=0) printf("(%d,%d)在X軸負(fù)方向n",x,y); else if(x=0 && y>0) printf("(%d,%d)在Y軸正方向n",x,y); else if(x=0 && y<0) printf("(%d,%d)在Y軸負(fù)方向n",x,y); else printf("(%d,%d)為坐標(biāo)原點(diǎn)n",x,y);(2)#include <stdio.h>#include <math.h>main() float a, b, c, x, s; printf("Input a, b and c:"); scanf("%f%f%f", &a, &b, &c); if(a+b<=c | a+c<=b | b+c<=a) printf("Error!n"); else x=(a+b+c)/2; s=sqrt(x*(x-a)*(x-b)*(x-c); printf("area=%fn", s); (3)程序代碼如下:#include <stdio.h>void main() float salary, tax; int ntax; printf("nPlease enter a salary="); scanf("%f",&salary); ntax=salary/500; if (ntax>=10) ntax=10; switch(ntax) case 0: case 1: tax=0;break; case 2: tax=(salary-1000)*0.05;break; case 3:case 4: tax=500*0.05+(salary-1500)*0.1;break; case 5:case 6: tax=500*0.05+1000*0.1+(salary-2500)*0.15;break; case 7:case 8:case 9: tax=500*0.05+1000*0.1+1000*0.15+(salary-3500)*0.20;break; case 10: tax=500*0.05+1000*0.1+1000*0.15+1500*0.20+(salary-5000)*0.30; printf("nThe tax=%10.2fn",tax); (4)方法一#include <stdio.h>#include <math.h>main() int i=1; float t=-1, s=0; do t= -t/i; s=s+t; i=i+1; while(fabs(t)>1e-6); printf("s=%fn", s); 方法二#include <stdio.h>void main() int i=1,sign=1; float t=1.0, s=0.0; while(1.0/t>1e-6) t= t*i; s=s+sign/t; i=i+1; sign=-sign; ; printf("s=%fn", s);(5)方法一:#include <stdio.h>main() int i, a, b, c; for(i=100; i<=999; i+) a=i /100; b=i/10%10; c=i%10; if(i=a*a*a+b*b*b+c*c*c) printf("%dn", i); 方法二:#include <stdio.h>void main() int i, a, b, c; for(a=1; a<=9; a+) for(b=0; b<=9; b+) for(c=0; c<=9; c+) i=a*100+b*10+c; if(i=a*a*a+b*b*b+c*c*c) printf("%dn", i); (6)#include <stdio.h>main() int i, j; for(i=1; i<=4; i+) for(j=1; j<=6-i; j+) putchar(' '); for(j=1; j<=i*2-1; j+) printf("%c", 65+i-1); printf("n"); for(i=3; i>=1; i-) for(j=1; j<=6-i; j+) putchar(' '); for(j=1; j<=i*2-1; j+) printf("%c", 65+i-1); printf("n"); (7)#include <stdio.h>main() int a, b, c; for(a=0; a<=20; a+) for(b=0; b<=33; b+) c=100-a-b; if(a*5+b*3+c/3.0=100) printf("%d, %d, %dn", a, b, c); (8)#include <stdio.h>#include <math.h>main() float x, x0, a; scanf("%f", &a); x=a; do x0=x; x=2/3.0*x0+a/(3*x0*x0); while(fabs(x-x0)>1e-6); printf("x=%fn", x);(9)#include <stdio.h>#include <math.h>main() float pi, pi0; int i=1 pi=2; do pi0=pi; pi=pi*(2.0*i/(2.0*i-1)*(2.0*i/(2.0*i+1); i=i+1; while(fabs(pi-pi0)>1e-6); printf("pi=%fn", pi);(10)#include <stdio.h>void main() int i,j,s; for(i=6;i<=1000;i+) s=1; for(j=2;j<=i/2;j+) if(i%j=0) s+=j; if(s=i) /*判斷是否是合數(shù)*/ printf("%d=1",i); /* 打印輸出合數(shù)及=1*/ for(j=2;j<=i/2;j+) /* 打印輸出各因子*/ if(i%j=0) printf("+%d",j); printf("n"); (11)#include <stdio.h>#include <math.h>void main() int x, y,z,k; k=sqrt(2000); for(x=0;x<=k;x+) for(y=0;y<=k;y+) for(z=0;z<=k;z+) if(x*x+y*y+z*z=2000) printf("%d,%d,%dn",x,y,z);