华农C语言教材习题参考答案

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1注:答案仅供参考分期还款:#includestdio.h#includemath.hintmain(){doubled,p,r;scanf(%lf%lf%lf,&d,&p,&r);printf(%.1f,log10(p/(p-d*r))/log10(1+r));return0;}计算分段函数函数值:#includestdio.hintmain(){doublex;scanf(%lf,&x);if(x1)printf(%.2f,x);elseif(x=1&&x10)printf(%.2f,2*x-1);elseprintf(%.2f,3*x-11);}成绩等级评分:#includestdio.h#includemath.hintmain(){intscore;scanf(%d,&score);if(score100||score0)printf(error);elseif(score=90)printf(A);elseif(score=80)printf(B);elseif(score=70)printf(C);elseif(score=60)printf(D);elseprintf(E);return0;}220秒后时间:#includestdio.h#includestdlib.hintmain(){inth,m,s;scanf(%d:%d:%d,&h,&m,&s);s=s+20;if(s59){s=s%60;m++;if(m59){m=m%60;h++;if(h23)h=0;}}printf(%02d:%02d:%02d\n,h,m,s);return0;}分期还款(加强版):#includestdio.h#includemath.hintmain(){doubled,p,r;scanf(%lf%lf%lf,&d,&p,&r);if(d==0)printf(0.0\n);elseif(d*(1+r)-pd)printf(God\n);else{printf(%.1f,log10(p/(p-d*r))/log10(1+r));}return0;}字母分类统计:#includestdio.h3#includemath.hintmain(){charch;intnL=0,nN=0,nS=0,nO=0;while((ch=getchar())!='\n'){if(ch='a'&&ch='z'||ch='A'&&ch='Z')nL++;elseif(ch='0'&&ch='9')nN++;elseif(ch=='')nS++;elsenO++;}printf(%d%d%d%d\n,nL,nN,nS,nO);return0;}计算数列和:#includestdio.hmain(){inti,n;doublea=2,b=1,s=0;scanf(%d,&n);for(i=1;i=n;i++){s=s+a/b;a=a+b;b=a-b;}printf(%.4f\n,s);}自由落体:#includestdio.h#includemath.hintmain(){doublesum=-100,d=100;inti,n;scanf(%d,&n);for(i=1;i=n;i++){4sum+=2*d;d/=2;}printf(%.3f%.3f,sum,d);return0;}迭代法求平方根:#includestdio.h#includemath.hintmain(){doublea,x0,x1=1;scanf(%lf,&a);do{x0=x1;x1=1.0/2*(x1+a/x1);}while(fabs(x1-x0)=1e-5);printf(%.4lf\n,x1);return0;}打印三角形:#includestdio.h#defineabs(x)((x)=0?(x):-(x))intmain(){intn,i,j;scanf(%d,&n);for(i=-(n-1);i=(n-1);i++){for(j=0;jabs(i);j++)printf();for(j=0;jn-abs(i);j++)printf(*);printf(\n);}return0;}勾股数:#includestdio.h#includemath.h5intmain(){inti,j,k,n;scanf(%d,&n);for(i=1;i=n;i++)for(j=i;j=n;j++){k=(int)sqrt(i*i+j*j);if(k*k==i*i+j*j&&k=n)printf(%d%d%d\n,i,j,k);}return0;}插入数据:intn,i;scanf(%d,&n);for(i=14;i=0;i--)if(a[i]n)a[i+1]=a[i];elsebreak;a[i+1]=n;输出不同的数:#includestdio.hmain(){inta[20];inti,t,p=0;for(i=0;i10;i++){scanf(%d,&a[i]);for(t=0;ti;t++)if(a[t]==a[i])break;if(t==i)printf(%d\n,a[i]);}}鞍点:#includestdio.h#includestdlib.hintmain(){inti,j,a[3][4],flag=0,col,r;for(i=0;i3;i++)for(j=0;j4;j++)scanf(%d,&a[i][j]);for(i=0;i3;i++)6{col=0;for(j=1;j4;j++)if(a[i][j]a[i][col])col=j;for(j=0;j4;j++){if(a[i][j]==a[i][col]){for(r=0;r3;r++)if(a[r][j]a[i][j])break;if(r==3){printf(%d\n,a[i][j]);flag=1;}}}}if(!flag)printf(NO\n);return0;}回文串:#includestdio.h#includestring.hmain(){inti,j;charbuf[100];scanf(%s,buf);for(i=0,j=strlen(buf)-1;ij;i++,j--)if(buf[i]!=buf[j])break;if(i=j)printf(Y);elseprintf(N);}ASCII码值之和的差:#includestdio.hintsum(char*s){intt=0,i=0;while(s[i]!='\0')t+=s[i++];7returnt;}intmain(){chars1[81],s2[82];gets(s1);gets(s2);printf(%d,sum(s1)-sum(s2));return0;}所有数字之和:#includestdio.hintsum(intn){intret=0;while(n0){ret+=n%10;n/=10;}returnret;}main(){intn;scanf(%d,&n);printf(%d,sum(n));}元音字母:#includestdio.hvoidyuan(char*s,char*s2){while(*s!='\0'){if(*s=='a'||*s=='o'||*s=='e'||*s=='i'||*s=='u'||*s=='A'||*s=='O'||*s=='E'||*s=='I'||*s=='U'){*s2=*s;s2++;}s++;}*s2='\0';}8main(){charstr[81],str2[81];gets(str);yuan(str,str2);printf(%s,str2);}字符统计:#includestdio.h#includestring.h#includemath.hintnL=0,nN=0;intstatistics(char*s){intret=0;while(*s!='\0'){if(*s='a'&&*s='z'||*s='A'&&*s='Z')nL++;elseif(*s='0'&&*s='9')nN++;elseif(*s=='')ret++;s++;}returnret;}intmain(){chars[81];intnS;gets(s);nS=statistics(s);printf(%d%d%d\n,nL,nN,nS);return0;}选择排序:#includestdio.h9intsort(inta[],intn){inti,j,k,tmp;for(i=0;in-1;i++){k=i;for(j=i+1;jn;j++)if(a[k]a[j])k=j;tmp=a[k];a[k]=a[i];a[i]=tmp;}}intmain(){inta[10];inti;for(i=0;i10;i++)scanf(%d,&a[i]);sort(a,10);for(i=0;i10;i++)printf(%d\n,a[i]);return0;}x的n次方:#includestdio.hintF(intx,intn){if(n==1)returnx;elsereturnF(x,n-1)*x;}intmain(){intx,n;scanf(%d%d,&x,&n);printf(%d,F(x,n));return0;}行列互换:#includestdio.hvoidswap(int*a,int*b){inttemp;10temp=*a;*a=*b;*b=temp;}voidcol(inta[][4],inti,intj){intk;for(k=0;k4;k++)swap(&a[k][i],&a[k][j]);}voidrow(inta[][4],inti,intj){intk;for(k=0;k4;k++)swap(&a[i][k],&a[j][k]);}intmain(){inta[4][4];inti,j;for(i=0;i4;i++)for(j=0;j4;j++)scanf(%d,&a[i][j]);col(a,0,2);row(a,0,2);col(a,1,3);row(a,1,3);col(a,0,3);row(a,0,3);for(i=0;i4;i++){for(j=0;j4;j++)printf(%d,a[i][j]);printf(\n);}return0;}学生信息统计:#includestdio.hvoidaverage(doublea[][5],intn){inti,j;doublesum;11for(i=0;in;i++){sum=0;for(j=0;j5;j++)sum+=a[i][j];printf(%.2f,sum/5);}printf(\n);}voidaverage2(doublea[][5],intn){inti,j;doublesum;for(j=0;j5;j++){sum=0;for(i=0;in;i++)sum+=a[i][j];printf(%.2f,sum/n);}printf(\n);}voidtop(doublea[][5],intn){inti,j;doublemax;for(j=0;j5;j++){max=a[0][j];for(i=1;in;i++)if(a[i][j]max)max=a[i][j];printf(%.2f,max);}printf(\n);}intmain(){doublea[10][5];inti,j;for(i=0;i10;i++)for(j=0;j5;j++)scanf(%lf,&a[i][j]);average(a,10);average2(a,10);top(

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功