《C语言课后习题.pdf》由会员分享,可在线阅读,更多相关《C语言课后习题.pdf(16页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、.习题习题第三章#include#include int main()int d,p;double r,a,m;scanf(%d%d%lf,&d,&p,&r);a=(p/(p-d*r);m=log(a)/log(1+r);if(p0&d0)if(d=m*p)printf(%.1f,m);elseprintf(God);elseprintf(0.0);return 0;勾股数#include int main()int a,b,c,n;scanf(%d,&n);for(a=1;a=n;a+)for(b=1;b=n;b+)for(c=1;c=n;c+)if(c*c=a*a+b*b)if(a=b)p
2、rintf(%d%d%dn,a,b,c);-优选.return 0;第四章计算分段函数值#include int main()float x,y;scanf(%f,&x);if(x=1&x=10)y=3*x-11;printf(%.2f,y);return 0;找出三个数中最大的数#include int main()int a,b,c,max;scanf(%d%d%d,&a,&b,&c);max=aba:b;if(cmax)max=c;printf(%d,max);return 0;10844 成绩等级评分#include int main()int x;scanf(%d,&x);if(x=
3、0&x=100)switch(x/10)case 10:-优选.case 9:printf(A);break;case 8:printf(B);break;case 7:printf(C);break;case 6:printf(D);break;default:printf(E);break;elseprintf(error);return 0;前续和后续字符#include int main()char x;x=getchar();switch(x)case0:printf(first);printf(%c,x+1);break;case9:printf(%c,x-1);printf(la
4、st);break;case1:case2:case3:case4:case5:case6:case7:case8:printf(%c,x-1);printf(%c,x+1);break;default:printf(error);break;return 0;20 秒后的时间#include#include int main()-优选.int h,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:%02dn,h,m,s);return 0;分期还款
5、#include#include int main()int d,p;double r,a,m;scanf(%d%d%lf,&d,&p,&r);a=(p/(p-d*r);m=log(a)/log(1+r);if(p0&d0)if(d=m*p)printf(%.1f,m);elseprintf(God);elseprintf(0.0);return 0;第五章字母分类统计-优选.#include#include int main()char ch;int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0;while(ch=getchar()!=n)if(ch=123)a+;
6、elseif(ch=97&ch=122)b+;else if(ch=91)c+;else if(ch=65)d+;else if(ch=58&ch=48&ch=33&ch=47)g+;else if(ch=32)h+;elsei+;printf(%d%d%d%d,d+b,f,h,a+c+e+g+i);水仙花数#include int main()int n,a,b,c;for(n=100;n1000;n+)a=n/100;b=(n%100)/10;c=n%10;if(n=a*a*a+b*b*b+c*c*c)printf(%dn,n);-优选.return 0;计算数列和#include int
7、 main()int a=1,n,i,sum=0;scanf(%d,&n);for(i=1;i=n;i+)sum=sum+a;a=a+2;printf(%d,sum);return 0;自由落体#include int main()int i,n;double h=100.0,s=0.0,c=100.0;scanf(%d,&n);s=s+c;h=h/2;c=2*h;printf(%.3lf,s);printf(%.3lf,h);return 0;迭代法求平方根#include#include int main()float x1,x2,a;x2=1;-优选.scanf(%f,&a);x1=a/
8、x2;while(fabs(x1-x2)=1e-5)x1=x2;x2=0.5*(x1+a/x1);printf(%.4fn,x2);return 0;打印三角形#include#include#includeint main()int n,k,i,j;scanf(%d,&n);for(i=0;i2*n-1;i+)k=abs(n-1-i);for(j=0;jk;j+)printf();for(j=0;jn-k;j+)printf(*);printf(n);return 0;勾股数#include int main()int a,b,c,n;scanf(%d,&n);for(a=1;a=n;a+)
9、-优选.for(b=1;b=n;b+)for(c=1;c=n;c+)if(c*c=a*a+b*b)if(a0;i-)if(aiai-1)t=ai;ai=ai-1;ai-1=t;大于平均分#include int main()int a10;int i,sum=0,average,count;for(i=0;i10;i+)scanf(%d,&ai);-优选.for(i=0;i10;i+)sum=sum+ai;average=sum/10;count=0;for(i=0;iaverage)count+;printf(%d,count);return 0;输出不同的数据#include int ma
10、in()int a20;int i,t,p=0;for(i=0;i10;i+)scanf(%d,&ai);for(i=0;i10;i+)for(t=0;ti;t+)if(at=ai)break;if(t=i)printf(%dn,ai);主对角线上的元素之和#includeint main()int a34,i,j,sum=0;for(i=0;i3;+i)-优选.for(j=0;j4;+j)scanf(%d,&aij);for(i=0;i3;+i)sum+=aii;printf(%dn,sum);return 0;1051 鞍点#include int main()int i,j,k,t=0,
11、flag=0,m=0,n=0,a34,max,min;for(i=0;i3;i+)for(j=0;j4;j+)scanf(%d,&aij);for(i=0;i3;i+)max=ai0;for(j=0;j4;j+)if(maxaij)max=aij;m=i;n=j;for(k=0;kakn)flag=1;if(flag=0)t=1;printf(%dn,amn);-优选.flag=0;if(t=0)printf(NO);return 0;ASCII 码值之和的差#include#include int main()char s180;char s280;int i,sum1=0,sum2=0;g
12、ets(s1);gets(s2);for(i=0;istrlen(s1);i+)sum1+=s1i;for(i=0;istrlen(s2);i+)sum2+=s2i;printf(%d,sum1-sum2);return 0;第七章所有数字之和#include int sum(int n)if(n10)return n;int res=0;res=(n%10)+sum(n/10);return res;main()-优选.int n;scanf(%d,&n);printf(%d,sum(n);元音字母#include#include void yuan(char*s,char*s2)int i
13、,k=0;char c;for(i=0;*(s+i)!=0;i+)c=*(s+i);switch(c)casea:casee:casei:caseo:caseu:caseA:caseE:caseI:caseO:caseU:*(s2+k)=c;k+;break;default:break;*(s2+k)=0;int main()char str81,str281;gets(str);yuan(str,str2);printf(%s,str2);字符统计#include#include#include-优选.int nL=0,nN=0;int statistics(char*s)int nS=0,
14、a;while(*s!=0)a=*s;if(a=)nS+;elseif(a=1&a=a&a=A&a=Z)nL+;s+;return nS;int main()char s81;int nS;gets(s);nS=statistics(s);printf(%d%d%dn,nL,nN,nS);return 0;选择排序#include int sort(int a,int n)int i,j,k,tmp;for(i=0;in-1;i+)k=i;for(j=i+1;jn;j+)if(ajak)k=j;tmp=ak;ak=aj;ai=tmp;int main()-优选.int a10;int i;fo
15、r(i=0;i10;i+)scanf(%d,&ai);sort(a,10);for(i=0;i10;i+)printf(%dn,ai);return 0;X 的 N 次方#include int F(int x,int n)if(n=0)return 1;else return x*F(x,(n-1);int main()int x,n;scanf(%d%d,&x,&n);printf(%d,F(x,n);return 0;行列互换#include void swap(int*a,int*b)int temp;temp=*a;*a=*b;*b=temp;void col(int a4,int
16、i,int j)int k;for(k=0;k4;k+)swap(*(a+k)+i),(*(a+k)+j);void row(int a4,int i,int j)int k;for(k=0;k4;k+)swap(*(a+i)+k),(*(a+j)+k);int main()-优选.int a44;int i,j;for(i=0;i4;i+)for(j=0;j4;j+)scanf(%d,&aij);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+)print
17、f(%d,aij);printf(n);return 0;学生信息统计#include void average(double a5,int n)int i,j;for(i=0;in;i+)double sum=0,av;for(j=0;j5;j+)sum=sum+aij;av=sum/5.0;printf(%.2f,av);printf(n);void average2(double a5,int n)int i,j;for(j=0;j5;j+)double sum=0,av;for(i=0;in;i+)-优选.sum=sum+aij;av=sum/n;printf(%.2f,av);printf(n);void top(double a5,int n)int i,j;double max;for(j=0;j5;j+)max=a0j;for(i=0;imax)max=aij;printf(%.2f,max);int main()double a105;int i,j;for(i=0;i10;i+)for(j=0;j5;j+)scanf(%lf,&aij);average(a,10);average(a,10);top(a,10);return 0;-优选
限制150内