c語言課后習(xí)題答案_第1頁
c語言課后習(xí)題答案_第2頁
c語言課后習(xí)題答案_第3頁
c語言課后習(xí)題答案_第4頁
c語言課后習(xí)題答案_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、7#230 5950519C語言程序課后答案1.void fun(int x) if(x>0) printf("%-4d",x); else printf("%-2d",0);#include<conio.h>main() int a6,i; clrscr(); for( i=0;i<6;i+) scanf("%dn", &ai); for(i=0;i<6;i+) fun(ai)2#include <stdio.h>#include <math.h>#include <

2、;conio.h>main() float sum=0.0,a,b,x1=0.0,y1=0.0,xx=0.0; float x12,y12,ma,na,mb,nb; int i; clrscr(); x0=0;y0=0; x1=5;y1=0.000127; x2=10,y2=0.000216; x3=15;y3=0.000286; x4=20;y4=0.000344; x5=25;y5=0.000387; x6=30;y6=0.000415; x7=35;y7=0.000437; x8=40;y8=0.000451; x9=45;y9=0.000458; x10=50;y10=0.000

3、462; x11=55;y11=0.000464; for(i=0;i<12;i+) sum+=xi*yi; x1+=xi; y1+=yi; xx+=xi*xi; printf("sum=%e,x1=%f,y1=%e,xx=%fn",sum,x1,y1,xx); ma=(sum*12-y1*x1);na=(xx*12-x1*x1); printf("ma=%f,na=%fn",ma,na); a=ma/na; mb=(xx*y1-x1*sum);nb=(xx*12-x1*x1); printf("mb=%f,nb=%fn",mb

4、,nb); b=mb/nb; printf("a=%e,b=%en",a,b);3。#include <stdio.h>#include <math.h>#include <conio.h>main() int i,j,b=0; clrscr(); for(i=0;i<3;i+) for(j=0;j<2;j+)if(i>=j) b+; printf("b=%dn",b);4。#include <stdio.h>#include <math.h>#include <coni

5、o.h>main() int j,x,max,min,sum; float aver; clrscr(); printf("please input 10 numbers:n"); scanf("%d",&x); sum=max=min=x; for(j=2;j<=10;j+) scanf("%d",&x); sum+=x; if(max<x) max=x; else if(min>x) min=x; sum=sum-max-min; aver=sum/8.0; printf("The

6、 average is:%6.2fn",aver);5#include <stdio.h>#include <math.h>#include <conio.h>main() int i,sum=0; clrscr(); for(i=1;i<=100;i+) if(i%7=0) sum+=i; printf("sum=%dn",sum);6。#include <stdio.h>#include <math.h>#include <conio.h>main() long n=1; float

7、 term=1.0,pi,sum=0.0; clrscr(); term=1.0/(n*n); while(term>=1e-12) term=1.0/(n*n); sum+=term; n+; pi=sqrt(sum*6); printf("pi=%fn",pi);7。#include <stdio.h>#include <math.h>#include <conio.h>main() int n=0; char ch; ch=getchar(); while(ch>32) n+; ch=getchar(); printf(

8、"n=%dn",n);8#include <stdio.h>#include <math.h>#include <conio.h>main() int i,j,k,n=0; clrscr(); for(i=5;i<=17;i+) for(j=5 ;j<=40;j+) k=100-i*5-j*2; if(k>4) n+; printf("n=%dn",n);9#include <stdio.h>#include <conio.h>main() int m,n,k; clrscr(

9、); for(n=2;n<=1000;n+) k=0; for(m=1;m<n;m+) if(n%m=0) k+=m; if(k=n) printf("%3d Its factors are:",n); for(m=1;m<n;m+) if(n%m=0) printf("%d,",m); printf("n"); 10#include <stdio.h>#include <math.h>#include <conio.h>main() char ch; int n=0; ch=ge

10、tchar(); while(ch!='n') if(ch>='0'&&ch<='9') n+; ch=getchar(); printf("n=%dn",n);11#include <stdio.h>#include <math.h>#include <conio.h>main() long int i,j,jc,n,sum; n=20;sum=0; clrscr(); for(i=1;i<=n;i+) jc=1; for(j=1;j<=i;j+)j

11、c=jc*j; sum+=jc; printf("sum=%ldn",sum);12#include <conio.h>#include <stdio.h>main() long int i,k,l,found; clrscr(); for(i=1;i<1000;i+) found=-1; k=i; l=i*i; while(k!=0) if(l%10!=k%10) found=0; break; k/=10; l/=10; if(found!=0) printf("%5ld,%8ldn",i,i*i); 13#includ

12、e <stdio.h>#include <math.h>#include <conio.h>main() int i,j,z,y,f,m,x1,x2; clrscr(); printf("Please input a number:n"); scanf("%d",&m); printf("%d=",m); for(i=2;i<=m/2;i+=2) f=1;for(j=2;j<=sqrt(i);j+) if(i%j=0) f=0; break; if(f=1) y=m/i; if(

13、y!=1) f=1; for(j=2;j<=sqrt(y);j+) if(y%j=0) f=0; break; if(f=1&&y!=1) z=m/(y*i); if(z!=1) f=1; for(j=2;j<=sqrt(z);j+) if(z%j=0) f=0;break; if(f=1&&z!=1) x1=m/(i*y*z); if(x1!=1) f=1; for(j=2;j<=sqrt(x1);j+) if(x1%j=0) f=0;break; if(f=1&&x1!=1) x2=m/(i*y*z*x1); if(x2!=

14、1) f=1; for(j=2;j<=sqrt(x2);j+)if(x2%j=0) f=0;break; elseif(f=1) printf("%d=%d*d%*%d*%d*%dn",m,i,y,z,x1,x2); break; 14#include <stdio.h>#include <math.h>#include <conio.h>main() int i,j,k,n=0; clrscr(); for(i=1;i<=20;i+) for(j=1;j<=50;j+)for(k=1;k<=100;k+) if(

15、i*5+j*2+k)=100) n+; printf("n=%dn",n);15#include <stdio.h>#include <math.h>#include <conio.h>main() int i,j,k=0; float n=0; clrscr(); for(i=1;i<100;i+) if(i>10) k+=(i*i/10)%10; n+=k*( 1e+i); if(i=n) printf("%d=>%dn",i,i*i); else k=(i*i)%10; if(i=k) prin

16、tf("%d=>%dn",i,i*i); 1662。C#include <stdio.h>#include <math.h>#include <conio.h>main() float a11=0,aver=0.0,i,max,min,sum=0.0; clrscr(); printf("Please input float number:n"); max=min=sum=a1; for(i=1;i<=10;i+) scanf("%f",&ai); max=min=sum=a1;

17、 for(i=2;i<=10;i+) sum+=ai; if(max<ai)max=ai; if(min>ai)min=ai; aver=sum/10; printf("sum=%-6.2f,aver=%-6.2fn",sum,aver); printf("max=%-6.2f,min=%-6.2fn",max,min);17#include <stdio.h>#include <math.h>#include <conio.h>main() int a44=0,0,0,i,j,sum=0; clrs

18、cr(); printf("Please input 9 numbers:n"); for(i=1;i<=3;i+) for(j=1;j<=3;j+) scanf("%d",&aij); if(i=j) sum+=aij; printf("sum=%dn",sum);186-C4#include <stdio.h>#include <math.h>#include <conio.h>main() float temp,a11=0.0,12.2,35.5,85.6,94.7,64.

19、8,43.1,100.5,78.6,25.1,56.4; int i,j; clrscr(); for(i=1;i<=9;i+) for(j=i+1;j<=10;j+) if(ai<aj) temp=ai;ai=aj;aj=temp; printf("%-7.1f",ai); printf("%-7.1fn",a10);196-5C#include <stdio.h>#include <math.h>#include <conio.h>main() char ch,s20; int i,l; clrs

20、cr(); ch=getchar(); scanf("%s",s); l=strlen(s); for(i=0;i<=l;i+) if(ch=si) break; printf("found! %dn",i);206-6C#include <stdio.h>#include <math.h>#include <conio.h>main() int i,j,k,c33=0; int a34=0,0,0,0,0,1,2,3,0,4,5,6; int b43=0,0,0,0,1,2,0,3,4,0,5,6; clrsc

21、r(); for(i=1;i<=2;i+) for(j=1;j<=2;j+) for(k=1;k<=3;k+) cij+=ajk*bkj; printf("The c Matrix:n"); for(i=1;i<=2;i+) for(j=1;j<=2;j+) printf("%6d",cij); printf("n"); printf("n");216-7C#include <stdio.h>#include <math.h>#include <conio

22、.h>main() int i,k,array11=0,2,4,6,8,10,1,3,5,7,9; clrscr(); for(i=1;i<=5;i+) k=arrayi; arrayi=arrayi+5; arrayi+5=k; for(i=1;i<=10;i+) printf("%4d,",arrayi); printf("b n");226-8C#include <stdio.h>#include <math.h>#include <conio.h>main() char s20; int i,

23、l; clrscr(); scanf("%s",s); l=strlen(s); printf("original string: "); printf("%sn",s); for(i=0;i<l;i+) if(si>='A'&&si<='Z') si+=32; else if(si>='a' && si<='z')si-=32; printf("change after string:")

24、; for(i=0;i<=l;i+) printf("%c",si); printf("n");23612。C#include <stdio.h>#include <math.h>#include <conio.h>main() int n2=0,i,j,k=2; clrscr(); for(i=0;i<k;i+) for(j=0;j<k;j+) nj=nj+1; printf("%dn",nk);1XT62。C#include <stdio.h>#include &l

25、t;math.h>#include <conio.h>main() float a11=0,aver=0.0,i,max,min,sum=0.0; clrscr(); printf("Please input float number:n"); max=min=sum=a1; for(i=1;i<=10;i+) scanf("%f",&ai); max=min=sum=a1; for(i=2;i<=10;i+) sum+=ai; if(max<ai)max=ai; if(min>ai)min=ai; av

26、er=sum/10; printf("sum=%-6.2f,aver=%-6.2fn",sum,aver); printf("max=%-6.2f,min=%-6.2fn",max,min);2XT63。C#include <stdio.h>#include <math.h>#include <conio.h>main() int a44=0,0,0,i,j,sum=0; clrscr(); printf("Please input 9 numbers:n"); for(i=1;i<=3;i+

27、) for(j=1;j<=3;j+) scanf("%d",&aij); if(i=j) sum+=aij; printf("sum=%dn",sum);3XT6-4。C#include <stdio.h>#include <math.h>#include <conio.h>main() float temp,a11=0.0,12.2,35.5,85.6,94.7,64.8,43.1,100.5,78.6,25.1,56.4; int i,j; clrscr(); for(i=1;i<=9;i+)

28、for(j=i+1;j<=10;j+) if(ai<aj) temp=ai;ai=aj;aj=temp; printf("%-7.1f",ai); printf("%-7.1fn",a10);4XT6-5。C#include <stdio.h>#include <math.h>#include <conio.h>main() char ch,s20; int i,l; clrscr(); ch=getchar(); scanf("%s",s); l=strlen(s); for(i=1;

29、i<l;i+) if(ch=si-1) break; printf("found! %dn",i);5XT66。C#include <stdio.h>#include <math.h>#include <conio.h>main() int i,j,k,c33=0; int a34=0,0,0,0,0,1,2,3,0,4,5,6; int b43=0,0,0,0,1,2,0,3,4,0,5,6; clrscr(); for(i=1;i<=2;i+) for(j=1;j<=2;j+) for(k=1;k<=3;k+)

30、 cij+=ajk*bkj; printf("The c Matrix:n"); for(i=1;i<=2;i+) for(j=1;j<=2;j+) printf("%6d",cij); printf("n"); printf("n");6XT67。C#include <stdio.h>#include <math.h>#include <conio.h>main() int i,k,array11=0,2,4,6,8,10,1,3,5,7,9; clrscr();

31、for(i=1;i<=5;i+) k=arrayi; arrayi=arrayi+5; arrayi+5=k; for(i=1;i<=10;i+) printf("%4d,",arrayi); printf("b n");7XT68。C#include<math.h>main() int m,i=0,k; long s=0; scanf("%d",&m); while(m!=0) k=m%2; s=s+k*pow(10,i); m=m/2; i+; printf("%ldn",s);

32、8XT6-10。C#include <conio.h>#include <stdio.h>main() char s180,s240; int i=0,j=0; clrscr(); printf("please input string1:n"); scanf("%s",s1); printf("please input string2:n"); scanf("%s",s2); printf("n"); while(s1i!='0') i+; while(

33、s2j!='0') s1i+=s2j+; s1i='0' printf("output link after string:%sn",s1);9XT6-12。C#include <conio.h>#include <stdio.h>main() char s180,s240; int i=0,j=0,k=0; clrscr(); printf("please input string1:n"); scanf("%s",s1); printf("n"); whi

34、le(s1i!='0') if(s1i%2=0 && k%2=0) s2j=s1i; i+; j+; k+; else i+; k+; s2j='0' printf("%sn",s2);XT7-14。C#include <conio.h>#include <stdio.h>main() char s180,s240,*p1,*p2; int i,j,k; clrscr(); printf("please input string1:n"); scanf("%s",

35、s1); printf("please input string2:n"); scanf("%s",s2); printf("n"); p1=s1; p2=s2; j=*p1; for(i=1;i<strlen(s1);i+) if(j<*(p1+i) k=i; p1=s1; p2=s2; for(i=strlen(s1);i>=k;i-) *(p1+i)=*(p1+i-1); while(*p2+!='0') *(p1+k)=*p2+; p1+; k+; p1=s1; printf("%s

36、n",*p1);XT771。C#include <conio.h>#include <stdio.h>main() int a34=1,10,11,12,20,13,14,15,11,16,17,18; int i,j,max,(*p)4,n=12; clrscr(); p=a; printf("output original matrix:n"); for(i=0;i<3;i+) for(j=0;j<4;j+) printf("%5d",*(*(p+i)+j); printf("nn")

37、; p=a; max=*(*p); for(i=1;i<n;i+) if(*(*p+i)>max) max=*(*p+i); printf("output max=%-4dn",max);XT83。#include <conio.h>#include <stdio.h>#include <math.h>main() float sabc(int x,int y,int z); int a,b,c; float area; clrscr(); printf("please input a,b and c:n"

38、); scanf("%d,%d,%d",&a,&b,&c); area=sabc(a,b,c); printf("area=%-6.2fn",area);float sabc(int x,int y,int z) float s,sarea; s=(x+y+z)/2.0; sarea=sqrt(s*(s-x)*(s-y)*(s-z); return(sarea);XT8-4。C #include <conio.h>#include <stdio.h>#include <math.h>main()

39、 int amstl(int x); int k,i; clrscr(); for(i=100;i<=999;i+) k=i; if(i=amstl(k) printf("%5d",i); printf("n");int amstl(int x) int a,s=0; while(x!=0) a=x%10;s+=a*a*a;x/=10; return(s); XT85。C#include <conio.h>#include <stdio.h>#include <math.h>#include <string

40、.h>main() int hwsz(char x); char s40; clrscr(); printf("please input a string:n"); scanf("%s",s); if(hwsz(s) printf("%s:is hws.n",s); else printf("%s:is ont hws.n",s);int hwsz(char x) char *p; int i,l,flag=0; p=x; l=strlen(x); printf("%dn",l); i=l

41、/2; printf("%dn",i); while(i>0) if(*(p+i-1)=*p) flag=1; else flag=0;p+;i-; return flag;XT6-。C#include <conio.h>#include <stdio.h>#include <math.h>#include <string.h>main() float fsum(float x,int n); float average(float x,int n); int i; float a10,*p; clrscr(); pri

42、ntf("please input ten numbers:n"); p=a; for(i=0;i<10;i+) scanf("%f",(p+i); printf("n"); p=a; printf("sum=%-6.2fn",fsum(p,10); p=a; printf("average=%-6.2fn",average(p,10);float fsum(float x,int n) int i; float s=0; for(i=0;i<n;i+) s+=xi; return(

43、s);float average(float x,int n) int i; float s=0,aver; for(i=0;i<n;i+) s+=xi; aver=s/n; return aver;XT8-7。C#include <stdio.h>#include <math.h>#include <string.h>#include <conio.h>int zm=0,sz=0,kg=0,qt=0;main() int count(char c); char s80; clrscr(); printf("input strin

44、g:"); gets(s); printf("%sn",s); count(s); printf("zm=%d,sz=%d,kg=%d,qt=%dn",zm,sz,kg,qt);int count(char c) int i; for(i=0;ci!='0'i+) if(ci>='A'&&ci<='Z')|(ci>='a'&&ci<='z') zm+; else if(ci>='0'&am

45、p;&ci<='9') sz+; else if(strcmp(ci,' ')=0) kg+; else qt+;XT88。C#include <conio.h>#include <stdio.h>main() long int fib(int m); int n; clrscr(); printf("input a number:n"); scanf("%d",&n); printf("Fibonacci(%d)=%ldn",n,fib(n);long i

46、nt fib(int m) int i; long int f1=1,f2=1,f; printf("%5ld%5ld",f1,f2); if(m=1| m=2) return 1; else for(i=1;i<m-1;i+) f=f1+f2; f1=f2; f2=f; printf("%5ld",f); printf("n"); return(f); XT8-9。C#include <stdio.h>#include <math.h>#include <conio.h>void conve

47、rt(int(*q)4,int);int a34,b43;main() int i,j,a34=0,11,12,13,0,14,15,16,0,17,18,19; int (*p)4; clrscr(); printf("output before transpert matrix:n"); p=a; for(i=1;i<=2;i+) for(j=1;j<=3;j+) printf("%5d",*(*(p+i)+j); printf("n"); printf("n"); p=a; convert(p,3

48、); printf("output after transpert matrix:n"); p=b; for(i=1;i<=3;i+) for(j=1;j<=2;j+) printf("%5d",*(*(p+i)+j); printf("n"); void convert(int (*q)4,int n) int i,j; for(i=1;i<=n-1;i+) for(j=1;j<=n;j+) *(*(q+j)+i)=*(*(q+i)+j);XT8-81。C#include <conio.h>#in

49、clude <stdio.h>long int fib(int);main() long int bf; int n; clrscr(); printf("input a number:n"); scanf("%d",&n); bf=fib(n); printf("Fibonacci(%d)=%ldn",n,bf);long int fib(int m) if(m=1| m=2) return 1; else return fib(m-1)+fib(m-2);XT6-13。C#include <conio.h

50、>#include <stdio.h>main() char ch80,tran80; int i; clrscr(); printf("please input char:n"); scanf("%s",ch); printf("mima is:%sn",ch); printf("n"); i=0; while(chi!='0') if(chi>='A')&& (chi<='Z')trani=26+64-chi+1+64

51、; else if(chi>='a')&& (chi<='z') trani=26+96-chi+1+96; else trani=chi; i+; printf("origial text:"); printf("%sn",tran);XT72。C#include <stdio.h>#include <string.h>#include <conio.h>main() int a,b,c,*p1,*p2,*p3,temp; printf("Plea

52、se three numbers:n"); scanf("%d,%d,%d",&a,&b,&c); p1=&a;p2=&b;p3=&c; if(*p1<*p2) temp=*p1; *p1=*p2; *p2=temp; else if(*p1<*p3) temp=*p1; *p1=*p2; *p2=temp; else if(*p2<*p3) temp=*p2; *p2=*p3; *p3=temp; printf("output sort after number:n");printf("%d,%d,%dn",a,b

溫馨提示

  • 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)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論