《湘潭大学c语言课后习题答案.pdf》由会员分享,可在线阅读,更多相关《湘潭大学c语言课后习题答案.pdf(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、5.2#include void main()int a,b,c,num,n;printf(请输入一个三位数:n);scanf(%d,&num);c=num%10;b=(num/10)%10;a=num/100;if(a=b&a!=c)n=1;else if(a=c&a!=b)n=1;else if(b=c&a!=b)n=1;else n=0;if(n)printf(%dn,num);5.3#include#include void main()int x;double y;scanf(%d,&x);/*输入 x*/switch(x/10)case 0:y=cos(x+3.0);break;c
2、ase 1:y=pow(cos(x+7.5),2);break;case 2:y=pow(cos(x+4.0),4);break;printf(%lfn,y);5.4#include void main()int num,i,leap;scanf(%d,&num);if(num%2!=0)printf(是奇数n);if(num1)for(i=2;i=num/2;i+)if(num%i=0)leap=0;break;else leap=1;if(leap)printf(是 素 数n);else printf(不是素数n);else printf(是偶数n);5.5#include void ma
3、in()int a,b,c,i,n;for(i=0;i10000;i+)for(n=1;n10000;n+)a=i;b=a+n;c=b+n;if(a*c=5*b&b+c=8*a)printf(%d,%d,%dn,a,b,c);break;6.1#include#include void main()double m=1.0,p=0.0;int k=1,n=1;do m=1.0/n;n=n+2;if(k%2=0)m=(-1.0)*m;p=p+m;k+;while(fabs(m)=1e-6);p=p*4.0;printf(%lfn,p);6.2#include void main()int k,k
4、1=1,k2=1,k3;float p=0,m;for(k=1;k=20;k+)k3=k1+k2;m=(float)k3/k2;p=p+m;k1=k2;k2=k3;printf(%fn,p);6.3(1).#include void main()int i,j,k;for(i=0;i=4;i+)for(j=0;j=3-i;j+)printf();for(k=0;k=2*i;k+)printf(*);printf(n);for(i=0;i=3;i+)for(j=0;j=i;j+)printf();for(k=0;k=6-2*i;k+)printf(*);printf(n);(2.)#includ
5、e void main()int i,j,k;char c=65;for(i=0;i=4;i+)for(j=0;j=3-i;j+)printf();for(k=0;k=2*i;k+)printf(%c,c);printf(n);c=c+1;for(i=0;i=3;i+)for(j=0;j=i;j+)printf();for(k=0;k=6-2*i;k+)printf(%c,c);printf(n);c=c+1;6.4#include void main()int a,b,i,n;for(a=1;a3000;a+)for(b=0,i=1;i=a/2;i+)if(a%i=0)b=b+i;for(n
6、=0,i=1;i=b/2;i+)if(b%i=0)n=n+i;if(n=a&ab)printf(%d 和%dn,a,b);6.5#include#include void main()int num,a,b,c,p;for(num=100;num1000;num+)c=num%10;b=num/10%10;a=num/100;p=pow(a,3)+pow(b,3)+pow(c,3);if(num=p)printf(%dn,num);6.6#include void main()int m,i,n;for(i=2;i1000;i+)m=0;for(n=1;n=i/2;n+)if(i%n=0)m=
7、m+n;if(i=m)printf(完数:%d=,i);for(n=1;n=i/2;n+)if(i%n=0)printf(%d ,n);6.7#include#include void main()int i,a,b;for(i=1000;i=9999;i+)a=i%100;b=i/100;if(i=pow(a+b),2)printf(%dn,i);6.8#include void main()int i,a,b,c;for(i=100;i1000;i+)a=i%10;b=i/10%10;c=i/100;if(a=b)&(b!=c)|(a=c)&(b!=c)|(b=c)&(a!=b)print
8、f(%dn,i);6.9#include void main()int i,n=1,m;for(i=1;i10;i+)n=(n+1)*2;printf(第一天摘了%d 个桃子n,n);7.1#include void main()int long a;char h16;int i;scanf(%ld,&a);i=15;while(a)int x=a%16;if(x10)hi=x+0;else hi=x-10+A;a=a/16;i-;printf(十六进制:);i+;while(i16)printf(%c,hi);i+;printf(n);7.2#include int main()int nu
9、m,a10,m;int i=0,j,flag=1;scanf(%d,&num);do m=num%10;ai=m;num=num/10;i+;while(num%10!=0);for(j=0,i-;i=j;i-,j+)if(aj=ai)continue;else flag=0;break;if(flag)printf(是回文n);else printf(不是回文n);return 0;7.3#include void main()int i,j,max,row=0,column=0;int a34=1,3,5,7,2,6,8,9,11,22,33,10;max=a00;for(i=0;i=2;
10、i+)for(j=0;jmax)max=aij;row=i;column=j;printf(max=%d,row=%d,column=%dn,max,row,column);7.4#include void main()int f10=1,1;int i,pow=0;for(i=2;i=9;i+)fi=3*fi-1+2*fi-2;for(i=0;i=9;i+)pow=pow+fi;printf(第十项是:%d,前十项和为:%dn,f9,pow);7.5#include void main()int i,j;int a88;for(i=0;i8;i+)ai0=1;aii=1;for(i=2;i8
11、;i+)for(j=1;ji;j+)aij=ai-1j-1+ai-1j;for(i=0;i8;i+)printf(%*d,18-i*2,ai0);for(j=1;j=i;j+)printf(%4d,aij);printf(n);7.6#include#include void main()int i,m;char a100,b100;gets(a);gets(b);if(strcmp(a,b)!=0)if(strlen(a)strlen(b)strcat(a,b);puts(a);else strcat(b,a);puts(b);8.1#include void Fun(int a)char
12、h16;int i;i=15;while(a)int x=a%16;if(x10)hi=x+0;else hi=x-10+A;a=a/16;i-;printf(十六进制:);i+;while(i16)printf(%c,hi);i+;printf(n);void main()int n;while(scanf(%d,&n)Fun(n);printf(n);8.6/(1.字符串的复制)/#include void strcpy(char*c,char*d)int i=0;while(*(d+i)!=0)*(c+i)=*(d+i);i+;*(c+i)=0;void main()char a100,
13、b100;printf(输入一个字符串:);gets(b);strcpy(a,b);printf(复制后的字符串:%sn,a);/(2.字符串的连接)/#include void strcat(char*c,char*d)while(*c!=0)c+;while(*d!=0)*c=*d;c+;d+;*c=0;void main()char a100,b100;printf(分别输入字符串 a 和 b:n);gets(a);gets(b);strcat(a,b);printf(连接后的字符串:n);puts(a);/(3.字符串小写字母改为大写字母)/#include void strupr(c
14、har a100)int i;for(i=0;i=97&ai=122)ai=ai-32;void main()char a100;printf(输入字符串:n);gets(a);strupr(a);printf(转化后的字符串:n);puts(a);/(4.字符串大写字母改为小写字母)/#include void strlwr(char a100)int i;for(i=0;i=65&ai=90)ai=ai+32;void main()char a100;printf(输入字符串:n);gets(a);strlwr(a);printf(转化后的字符串:n);puts(a);/(5.比较两字符串
15、是否相等)/#include int strcmp(char*c,char*d)while(*c=*d&*c!=0&*d!=0)c+;d+;if(*c=*d)return 0;else if(*c*d)return 1;else return-1;void main()char a100,b100;printf(分别输入两个字符串a和b:n);gets(a);gets(b);if(strcmp(a,b)=0)printf(相等n);else if(strcmp(a,b)=1)printf(不等,且 abn);else printf(不等,且 ban);8.7#include#include f
16、loat A(float a,float b,float c)float s;s=(a+b+c)/2.0;return(s);float B(float a,float b,float c)float area,s;s=A(a,b,c);area=sqrt(s*(s-a)*(s-b)*(s-c);return(area);void main()float a,b,c,area,s,p;printf(输入三边长 a,b,c:n);scanf(%f%f%f,&a,&b,&c);if(a+bc|a+cb|b+ca)printf(不能构成三角形n);else p=B(a,b,c);printf(三角形
17、面积是:%fn,p);8.10#include#include int fun(int x)int i,m=1;for(i=1;i=x;i+)m=m*i;return(m);void main()int n=10,sum=0;for(n=1;n=10;n+)sum=sum+fun(n);printf(和为:%dn,sum);8.11#include#include void fun(char a)char i,m,n;n=i=strlen(a);while(i3)i=i-3;for(m=n;mi;m-)am=am-1;ai=,;n+;void main()static char a20;printf(输入数字:n);gets(a);fun(a);puts(a);8.14#include#include long fun(char*a)int i=1,x,y;long m=0;x=strlen(a);if(a0=-)y=-1;x-;a+;else y=1;while(*a)m=m*10-48+(*a+);return(m*y);void main()char a100;long n;printf(输入纯数字字符串:n);gets(a);n=fun(a);printf(%ldn,n);