C語言課后答案1_第1頁
C語言課后答案1_第2頁
C語言課后答案1_第3頁
已閱讀5頁,還剩26頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、C語言課后答案(無11章)2. 1 D2.2 #include<stdio. h>main() float x=2. 5, y=2. 5, z=2. 5; printf("x=%fn ”,x); printf Czy=%fn',y);printf ( z=%fn , z);第三章3. 1 (float) (a*b) /2二 0 (int)x%(int)y=23.2 #include<stdio. h> main() (int x, bO, bl, b2, s;printf("Inputx:" ) ; scanf ( %d , &am

2、p;x);b2=x/100;bl=(x-b2*100)/10;b0=x%10;s 二 b0*100+bl*10+b2;printf (" s 二dn,s);)3.3 #include<stdio. h>#include<math. h>main()(float rate=0. 0225;float n, capital, deposit;printf ( z,Input n, capital:");scanf (" %f, %f " , &n, capital);deposit 二 capital*pow(l+rate,

3、n);printf ( z/ deposit=%fn z/, deposit);3.4 #include<stdio. h> #include<math. h> main()float a, b, c;double x,y;printf ("Inputa, b, c:");scanf ("%f, %f, %f " , &a, &b, &c);x=(-b+sqrt(b*b-4*a*c)/(2*a);y= (-b-sqrt(b*b-4*a*c)/(2*a);printf fx=%f, y=%fn ",

4、 x, y);第四章:4. 1 (1) aabbcc 12%, 15% (3) 12, 564.2 #include<stdio. h>main()long a, b;float x, y;scanf ("%d, %dn " , &a, &b);scanf ("%f, %fn ", &x, &y);printf (" a=%d, b=%dn " , a, b);printf (" x=%f, b=%fn ", x, y) ;第五章:5. 1 #include<std

5、io. h>main()float a;printf("Innputa:");scanf ("%f ” , &a);if (a>=0)a=a; printf ( z,a=%fn,z, a) ;elsea=-a; printf ("a=%fn ", a) ;5.2 #include<stdio. h> main() int a; printf ( /z Inputa: z/);scanf ( %c , &a);if(a%2=0)printf (z,a 是偶數(shù));else(printf (,za 是奇數(shù));5

6、.3 #include<stdio. h>#include<math. h>main ()(float a, b, c, s, area;printf ( Inputa, b, c:);scanf ("%f, %f, %f ” , &a, &b, &c);if(a+b>c&&a+cb&&b+c>a) s=(a+b+c)/2; area=(float)sqrt(s*(sa)*(sb) *(sc); printf ( z,area=%fn ,z, area);elseprintf ( 不是三角形)

7、;5.4 #include<stdio. h>#include<math. h>main ()(float a, b, c, x, y;printf ( z,Inputa, b, c: );scanf ("%f, %f, %f ,&a, &b, &c); if (a=0)printf("該方程不是一兀二次方程n");if(b*b-4*a*c>0) x=(-b+sqrt(b*b-4*a*c)/(2*a);y=(-b-sqrt(b*b-4*a*c)/(2*a);printf ( /z x=%f, y=%fn /z,

8、 x, y);else if(b*b-4*a*c=0)!x=-b/ (2*a);y=-b/(2*a);printf ( x=%f, y=%fn , x, y);else(printf( ,z該方程無實根rT) ;5.5 #include<stdio. h>main()(int year, flag;printf( ,zlnput a year:");scanf ( d , &year);if (year%4 0&&year%400! =01 | year%400 0)flag。;elseflag=O;if (flag=l)printf( d is

9、a leap year !n,year);elseprintf( d is not a leap year !n,year);5.6#include<stdio. h>main () int year, flag; printf( z,Input a year:");scanf ( d , &year);f 1 ag=year%400=01 | year%40&&year%100!=0?l :0;if (f lag=l&&f lag! =0)printf (,z%d is a leap year ! n ,: year);elsep

10、rintf (z/%d is not a leap year ! n/z, year) ;5.7 #include<stdio. h>main ()(char ch;printf("Inputch: );scanf ( %c , &ch);if (ch>='a &&ch<=' z')!ch=getchar();ch 二 ch-32;printf ( %c, %dn , ch, ch);else if (ch>='A&&ch<='Z')(ch=getchar();

11、ch=ch+32;printf ( %c, %dn , ch, ch);else(printf( c,ch);5. 8 #include<stdio. h>main () char ch;printf("Inputch:); scanf ( c,&ch); if(ch>=48&&ch<=57)printf( " ch 是數(shù)字字符 n ); else if(ch>=65&&ch二 90)(printf (,zch 是大寫字母 n"); else if(ch>=97&&ch&l

12、t;=122)printf (z/ch 是小寫字母 n"); else if(ch=32)printf ( z,ch 是空格 n ); else(printf ( z/ch 是其他字符 n ); 5.9 #include<stdio. h> main ()( d , &score);int score, grade;printf( z, Input score:"); scanf ( grade=score/10;, ? , z if(score<0|score>100) printf ( Input errorn ) ; if(score&

13、gt;=90&&score<=100) printf ( z/ %d 一 An , score) ; else if(score>=80&&score<90)printf( %d-Bn , score) ;else if(score>=70&&score<80)printf( %d-Cn , score);%d En, score) ;else if(score>=60&&score<70) printf (z/ %d 一 Dn , score) ; else if(score>=0

14、&&score<60) printf(5. 10 #include<stdio. h> main () int year, month;printf ( Input year, month: ) ; scanf (d, %d ”, &year,&month);if(month>12|month<=0)(printf (z,error monthn,z) ;else(switch(year, month)case 12:case 10:case 8:case 7:case 5:case 3:case 1:printf (z,31 天

15、n );break;case 11:case 9:case 6: case 4: printf( 30 天n );break;case 2:if (year%4 0&&year !=0 | | year%400=0)(printf (,z29 天n") ;else(printf (,z28 天n );break;default:printf ( z,Input errornz,) ;)第六早:6.1(1)10.4 k=3,n=310.5 k=4,n=06.2(1) #include<stdio.h> main() int i,sum=O;for(i=l;i

16、<=101;i+)sum=sum+i;printf("sum=%dn :sum);6.2(2) #include<stdio.h>main()(long i ;long term,sum=0;for(i=l;i<=101;i=i+2)term 二 i*(i+l)*(i+2);sum=sum+term;printf("sum=%ldn",sum);6.2(4) #include<stdio.h>#include<math. h> main()(int n=l;float term= 1.0,sign= 1 ,sum=0

17、;while(term<=- le-4|term>= 1 e-4)(term=1.0/sign;sum=sum+term;sign=sign+n;n+;printf("sum=%fn",sum);6.2(5) #include<stdio.h>#include<math. h>main()(int n=l,count=l;float x;double sum,term;printf( “Input x: n);scanf( H%f*,&x);sum=x;term=x;doterm=-term*x*x/(n+1) *(n+2);su

18、m=sum+term;n=n+2;count+;)while(fabs(term)>= le-5);printf( H sin(x)=%f,count=%dnH,sum,count);12.4 #include<stdio.h>main()(int x=l,find=0;while(!find)if(x%2=l&&x%3=2&&x%5=4&&x%6=5&&x%7=0)printf(nx=%dn",x);find=l;/* int x,find=O;for (x=l;!find;x+)if(x%2=l&a

19、mp;&x%3=2&& x%5=4&& x%6=5&& x%7=0)(printf("x=%dnu,x);find=l;*/12.5 #include<stdio.h>main()(int i,n;long p=l,m=l;printf(uInput n: n);scanf("%d",&n);for(i=l;i<=n;i+)p=i*i;m=i*i*i;npri ntf(p=%d,m=%dn" ,i,p,i,m);12.6 #include<stdio.h>mai

20、n()float c,f;for(c=-40;c<=l 10;c=c+10)f=9/5*c+32;printf("f=%fn",f);12.7 |#include<stdio.h>#include<math.h>main()int n;double c=0.01875,x;dox=x*pow( 1 +c,l 2)-1000;n+;)while(x>0);printf("x=%dnu,x);12.8 #include<stdio.h>main()(int n=0;float a=100.0,c;nHprintf( In

21、putc: );scanf("%f,&c);do(a=a*(l+c);n+;)while(a<=200);printf(" n=%dn" ,n);12.9 #include<stdio.h>#include<math. h>main() int n=l,count=l;double sum= 1 ,term= 1;while(fabs(term)>= 1 e-5)term=pow(-1 ,count) *( 1.0/(n+2);sum=sum+term;n=n+2;count+;sum=4*sum;printf(&quo

22、t; sum=%f,count=%dn",sum,count);12.10 #include<stdio.h>#include<math. h>main()int n=l,count=l;double sum= 1 ,term= 1;while(fabs(term)>= 1 e-5)term=term*(l .0/n);sum=sum+term;n+;count+;printf( ” sum=%f,count=%d n",sum,count);12.11 #include<stdio.h>#include<math. h>

23、;main()(int x;for(x= 100 ;x<=999;x+)(if(x=pow(x/l 00,3)+pow(x/10%10,3)+pow(x% 10,3) printf(n ” , x);12.12 #include<stdio.h>main() int i=0,n;long sum=O,term=l;printf(HI nputn: ”);scanf("%d",&n);doi+ ;term=term*i;sum=sum+term;while(sum<n);printfC ,%dnu,i-l);12.13 #include<

24、stdio.h>main()(int i,n,m,count=0,sum=0;printf( “Input n:n);scanf("%d",&n);for(i= 1 ;iv=n;i+)printf(H| nputm: ” );scanf("%d",&m);if(m>0)sum=sum+m;count+;else(break;printf("sum=%d,count=%dn",sum,count);12.14 #include<stdio.h>main()(int i,n,m,count=0,su

25、m=0;Hprintf( Inputn:");scanf("%d",&n);for(i=l;i<=n;i+)(printf( nInputm: n);scanf("%d",&m);if(m>0|m<0) sum=sum+m;count+;elsebreak;printf(Hsum=%d,count=%dn",sum,count);12.15 #include<stdio.h>main()(int x,y, z;for(x=0;x<=17;x+)for(y=0;y<=25 ;y+

26、)3*x+2*y+z=50;z=30_x_y;if(3*x+2*y+z=50&& x+y+z=30)printf("x=%d,y=%d,z=%dn",x,y,z);12.16 #include<stdio.h> main()(int x,y;for(x=0;xv=98;x+)(y=98_x;2*x+4*y=386;if(x+y=98&&2*x+4*y=386)(printf( ” x=%d,y=%d n” ,x,y);12.17 #include<stdio.h> main()(int x,y, z;for(x=0;x

27、<=20;x+) for(y=0;y<=33 ;y+)(3 *y+5 *x+z/3.0= 100;z=100-x-y;if (5 *x+3 *y+z/3.0= 100&&z+x+y= 100)(printf("x=%d,y=%d,z=%dn",x,y,z);12.18 #include<stdio.h>main()int x,y, z;fbr(x= 1 ;xv=9;x+)(for(y= 1 ;y<= 17 ;y+)10*x+5 *y+z=100;z=50-x-y;if( 10*x+5 *y+z= 100&& x+

28、y+z=5 0&&z>0) printf("x=%d,y=%d,z=%dn",x,y,z);第七章:#include<stdio. h> int hour, minute, second;void update()(second+;if (second=60)second=0;minute+;if (minute60)minute=O;hour+;if (hour=24)hour=0;void display()(printf ( d, %d, %dn , hour, minute, second); void delay ()(int t

29、;for(t=0;t<100000000;t+);int main ()(int i;void updaye (), display (), delay (); for(i=0;i<1000000;i+)(update ();display();delay ();return 0;#include<stdio. h>int GetMax(int a, int b);int main () int x, y, max;printf ( z, Inputx, y: );scanf ("%d, %d ”, &x, &y);max=GetMax(x,y

30、);printf( max=%dn, max);return 0;int GetMax (int m, int n)if(m>=n) return m; elsereturn n;#include<stdio. h> int LCM(int n, int m); int main ()(int a, b;printf ("Inputa, b: ); scanf ( d, %d , &a, &b); printf ( drT, LCM (a, b); return 0;int LCM(int n, int m) int x;int find=O;for

31、(x=l;!find;x+)(if (x%n 0&&x%m=0)(find=l;return xl;#include<stdio. h> long Fact (int n);int main () int m, a;printf ( /z Inputm: z/); scanf ( %d, &m); for(a=1;a<=m;a+)printf ( d!=%ldrT, a, Fact (a);return 0;long Fact(int n) int i;long result=l;for(i=2;i =n;i+)result* 二 i;return r

32、esult;#include<stdio. h> long Fact(int n);int main () int m;long ret;/z z/ printf ( Inputm: ); scanf ( %d, &m); ret=Fact(m);z/, zprintf ( ret=%dn , ret); return 0;long Fact(int n)(int i;long result=1, sum 二 0;for(i=2;i =n;i+)(result* 二 i; sum=sum+result;return sum;7. 7 (1) #include<stdi

33、o. h>int Gcd(int a, int b);int main ()int m, n;printf ( /z Inputm, n: , z);scanf ( d, %d ”, &m, &n);printf ("%dn , Gcd (m, n);return 0;int Gcd(int a, int b)int t, min, find=O;min=a<b?a:b;t=min;for(t=min;!find;t-)a%t=0;b%t=O;if(a%t=0&&b%t=0)return t;find=l;7. 7 (2) #include

34、<stdio. h>int Gcd(int a, int b);int main ()int m, n;printf ( Z/ Inputm, n: /z );scanf ("%d, %d”, &m, &c);printf ( dn , Gcd (m, n); return 0;int Gcd(int a, int b) int r, temp; r=a%b;if (r=0)return b;else do temp=b;b=r; a=temp; r=a%b;while(r! =0); return b;)第八章:8. 1(1) 10(2) 01238.

35、2(1) int PositiveNum(int a, int n) ( int i,count=0;for (i=0;i<n;i+)if (ai>0) count+;return 0;8. 2 (2) void Fib (long f , long n) int i;f0=0: fl=l ; for(i=2;i<n;i+) ( fi=fi-l+fi-2;)8. 2 (3) #include<stdio. h>int main ()int a 10, n, max, min, maxPos, minPos;for(n 二 0;n<10;n+) scanf (

36、%d, &an);max=min=a0;maxPos=minPos=0;for(n 二 0;n<10;n+)!if (an >max)(max=an;maxPos=n;else if(an<min)(min=an;minPos=n;printf (z, max=%d, pos=%dn , z,max, maxPos);printf (, min=%d, pos=%dn z,, min, minPos);return 0;void DivArray(int *pArray, int n) !int i;for (i=0;i<n;i+)(pArrayi/=pAtta

37、y0;#include<stdio. h> ttdefine N 40 int FailNum(int scored, int n);void ReadScore(int score,int n);int main () int scoreN, n;printf( z, Input n: ) ;scanf ( %d, &n);ReadScore (score, n);z, FailNum(score, n);FailNum(score, n);printf ( z/ FailNum students are %dn return 0;int FailNum(int scor

38、e, int n)(int i,count=0;for(i=0;i<n;i+)(if(scorei<60) count+;return count;void ReadScore (int score,int n)!int i;printf( z, Input score:");for(i=0;i<n;i+)(scanf( %d, &scorei);#include<stdio. h>ttdefine N 40int HighAver(int score, int n);int ReadScore(int score);int main ()( i

39、nt scoreN, n;, HighAver (score, n);n=ReadScore(score);printf ( z, HighAver students are %dn return 0;int HighAver(int score, int n)!int i, count=0, sum 二 0, aver; for(i=0;i<n;i+)!sum=sum+scorei; aver=sum/n;for(i=0;i<n;i+)(if(scorei>aver) count+;return count;int ReadScore(int score)!int i=-l

40、;do i+ ;printf( z, Input score:");scanf( %d, &scorei);while(scorei>=0);return i;#include<stdio. h>ttdefine N 40int ReadScore(int score, long num); int FindMax(int scored, long num, int n); int main ()int score N, n, max, num, t;long numN;t=FindMax (score, n);max=scoret;num=numt;pri

41、ntf ( /z max=%d, num=%dn, z, max, num);return 0;int ReadScore(int score, long num)(int i=-l;do i+ ;printf ( /z Input student ,s ID and score:"); scanf ( ld%d , &numi, &scorei); )while(numi>0&&scorei>=0);return i;int FindMax(int scored, long num, int n)(int max, i;max=score

42、0;for(i=l;i<n;i+)(if(scorei>max)max=score i;return i;#include<stdio. h>ttdefine N 40int Read(int a);int ChangeArry(int a, int n);int main ()int aN, n;printf ( dn , ChangeArry (a, n); return 0;int Read(int a)int i;for (i=0;i<9;i+)(z,printf ( Input a: ) ; scanf ("%d , &ai); ret

43、urn i;int ChangeArry(int a, int n)!int max, min, i, maxpos, minpos, term; max 二 min 二 score0;for(i=l;i<n;i+)(if(ai>max)max=score i;maxpos=n;else (ai <min) min=ai; minpos=n;temp=maxpos; maxpos=minpos;minpos=temp; return ai;#include<stdio. h> #define N 5 void main () (int aN N;int sum =

44、 0;int i, j;printf(請輸入一個%d*%d的矩陣n" , N, N);for(i = 0; i < N; i+) for(j = 0; j < N; j+)for (i0; i < N; i+)!or(j10; j < N; j+)!if (ij 1 i + j = N - 1)sum=sum+aij;printf(對角線元素之和為: n" , sum);#include<stdio. h>ttdefine N 2ttdefine M 3void main () int aN M,bN M;int i, j, suml=0

45、, sum2=0, sum=0;printf ( 請輸入一個 %d*%d 的矩陣 n”,N, M);for(i = 0; i<N; i+)(for(j =0; j< M; j+)(scanf & ai j);suml=suml+ai j;printf ( 請輸入一個 d*%d 的矩陣 n" , N, M);for(i = 0; i <N; i+)(for(j = 0; j <M; j+) scanf ("%d , &bi j);sum2=sum2+ai j;sum=suml+sum2;printf( sum=%dn, sum); #i

46、nclude<stdio. h>ttdefine N 7void YangHui(int aNN, int n);void main ()!int i, j, n, b N N;printf( ,zInput n: ) ;scanf ( %d, &n);YangHui (b, n);for (i=0;i<n;i+)(for(j=0;j<=i;j+)!printf ("%4d ”, bi j);printf( n) ;void YangHui(int aNN, int n)!int i, j;for (i=0;i<n;i+)!ai0=l;aii=l

47、;for(i=2;i<n;i+)(for(j=l;ji;j+)ai j=ai-l j-l+ai-l j;#include<stdio. h> ttdefine N 12 void main ()(int i;int =1);printf ( d:%drT, 0, f 0);n”,1, f 1);for(i=2;i<N;i+) !fi=fi-l+fi-2;printf ( , %d:%dn,/, i+1, f i); #include<stdio. h> #include<stdlib. h> #include<time. h> ttd

48、efine N 6000 void main () !int aN, i;int bl=0, b2=0, b3=0, b4=0, b5=0, b6=0; float pl, p2, p3, p4, p5, p6; srand(time(NULL);for(i=0;i<N;i+)( ai=rand()%6+l; printf ("%3d , ai);if (ai=l) bl+; if (ai=2) b2+;if (ai 二二 3) b3+;if (ai 二二 4) b4+;if(ai=5)b5+;if (ai=6) b6+; pl=(float) (bl/6000. 0); p2

49、= (float) (b2/6000. 0); p3= (float) (b3/6000. 0); p4= (float) (b4/6000. 0); p5= (float) (b5/6000. 0); p6= (float) (b6/6000. 0);printfC 骰子出 現(xiàn)1 printf(" 骰子出現(xiàn) 2 printfC 骰子出 現(xiàn)3 printfC 子出現(xiàn) 4printfC 骰子出 現(xiàn)5 printf 第 9章:(" 骰子出現(xiàn) 69.1(1) void Swapfint *x,int *y) (int *pTemp;/*pTemp*x=*y;的概率: %.2fW.

50、pl) 的概 率: %. 2fn z,, p2) 的概骰 率:n” ,p3) 的概率: .2fn",p4) 的概.2fn",ppTemp 指向哪里位置未進(jìn)行初始化,*pTemp=*x;*y=*pTemp;9.1(2) void Swap(int *x,int *y)(int *pTemp;pTemp=x;x=y;y=pTemp;錯誤, x,y 沒有指向類型#include<stdio.h>int main()(int a= 1,2,3,4,5;int *p=a;-,-(*p),*p);printf( ”n”,*p,*(+p),(*p)+,*p,*p return

51、 0;#include<stdio.h> void Swap(int *x,int *y);int main() int a6= 1,2,3,4,5,6,b6= 11,22,33,44,55,66; int i;printf( HBefore Array n");for(i=0;iv6;i+)(printf( n %dn",ai);fbr(i=0;i<6;i+)(printf("%dn n,bi); for(i=0;iv6;i+)Swap(&ai,&bi);printf( HAfter Array n");for(i=0

52、;iv6;i+)printf("%dn n,ai);fbr(i=0;i<6;i+)(printf("%dn n,bi);return 0;void Swap(int *x,int *y)(int temp; temp=*x;*x=*y;*y=temp;#include<stdio.h>#define N 10void FindMaxMin(int a,int *pMaxa,int *pMaxnum,int *pMina,int *pMinnum); int main()(int aN;int i,maxa,mina,maxnum,minnum;printf

53、("Input a:"); for(i=0;i<10;i+) scanf("%d",&ai);FindMaxMin(a,&maxa,&maxnum,&mina,&minnum);printf( Hmaxa=%d,maxnum=%d,mina=%d,minnum=%dn",maxa,maxnum,mina,minnum); return 0; void FindMaxMin(int a,int *pMaxa,int *pMaxnum,int *pMina,int *pMinnum)(int i;*pM

54、axa=a0;*pMina=a0;*pMaxnum=0;*pMinnum=0;for(i=0;i<10;i+)if(ai>*pMaxa)*pMaxa=ai;*pMaxnum=i;else if(ai<*pMina)(*pMina=ai;*pMinnum 二 i;)第 10 章:void MYStrcpy(char *dstStr,const char *srcStr)(while (*dstStr+=*srcStr+) !='0*)(#include<stdio.h>main()n(char *a= main()(char *a=% c% s% c; pr

55、intf(a,34,a,34);"printf(a,34,a,34);10.3(1) unsigned int MyStrlen(char *p)main()unsigned int len;len=O;for(; *pa!='0' ;pa+) len+;return len;10.3(2) unsigned int MyStrlen(char s)(char *p=s;while(*p!='0') (P+;return (p-*p);10.3(3) int MyStrcmp(char *pl,char *p2)for(; *p 1 =*p2 ;p 1 +,p2+)if(*pl=*0') return -1;return 1;10.3(4) int MyStrcmp(char s,char t)( int i;for(i=0;si=ti ;i+)(if( si= ,0, ) return 0;return i;10.3(5) #include<stdio.h>#include&

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論