《C语言程序设计(高潮版)编程题答案.docx》由会员分享,可在线阅读,更多相关《C语言程序设计(高潮版)编程题答案.docx(37页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、3-1#include int main ( ) float x,y; printf(Enter x:); scanf(%f,&x); if(x=0?x:-x); else if(x20) y=3*x*x-2*x+1; else y=x/5; printf(x=%f, y=%fn,x,y); return 0;3-2#include #include int main() int a,b,c;double delta,p,q;printf(请输入一元二次方程的系数a,b,c:);scanf(%d%d%d,&a,&b,&c);delta=b*b-4*a*c;p=-(double)b/(2*a);
2、q=sqrt(fabs(delta)/(2*a);if(delta=0)printf(nx1=%fnx2=%fn,p+q,p-q);elseprintf(nx1=%f+i%f,p,q);printf(nx2=%f-i%fn,p,q);return 0;3-3#include int main() int p,q; /q为转换点float s,tax;printf(Please enter s(工资额):);scanf(%f,&s);q=(int)s/1000;switch(q) case 0: p=0; break; case 1: p=5; break; case 2: case 3: ca
3、se 4: p=8; break; case 5: case 6: case 7: p=15; break; default:p=20;tax=s*p/100;s=s-tax;printf(纳税款: %5.0fn,tax);printf(实得工资数: %5.0fn,s);return 0;3-4#include int main() int i,j,k,sum=0;for(i=1;i=99;i+=2)j=i+1,k=i+2,sum=sum+i*j*k;printf(1*2*3+3*4*5+99*100*101=%dn,sum);return 0;3-5#include int main()do
4、uble e=1.0;double u=1.0;int n=1;while(u=1.0e-6)u=u/n;e=e+u;n=n+1;printf(e=%f (n=%d)n,e,n);return 0;3-6#include int main( )int i,t,sign=1,m=2,n=1;/sign用于设置正负号,m代表分母,n代表分子double u,sum=0;for(i=1;i=20;i+)u=sign*1.0*m/n;/or: u=sign*(double)m/n;sum=sum+u;sign=-sign;/每次循环改变一次正负号t=m,m=m+n,n=t;/当前的分子分母之和是后一项
5、的分子,当前的分子是后一项的分母printf(sum=%fn,sum);return 0;3-7/方法一#include int main( ) int g,s,b; /分别代表个位、十位、百位上的数码 for(b=1;b=9;b+) /百位上的数码由1变到9 for(s=0;s=9;s+) /十位上的数码由1变到9 for(g=0;g=9;g+) /个位上的数码由1变到9 if(g*g*g+s*s*s+b*b*b=g+s*10+b*100) printf(%dn,g+s*10+b*100); return 0;/方法二#include int main( ) int g,s,b; /分别代表
6、个位、十位、百位上的数码 int x; /x代表一个3位整数 for(x=100;x=999;x+) b=x/100; s=(x-b*100)/10; g=x%10; if(g*g*g+s*s*s+b*b*b=x) printf(%dn,x); return 0;*/方法三:移位求余得数码#include int main( ) int g,s,b; /分别代表个位、十位、百位上的数码 int x,y; /x代表一个3位整数 for(x=100;x=999;x+) y=x; g=y%10; s=(y=y/10)%10; b=(y=y/10)%10; if(g*g*g+s*s*s+b*b*b=x
7、) printf(%dn,x); return 0;3-8/方法一#include int main( ) int num=0,g,s,b;/g,s,b分别代表个位、十位、百位上的数码(1000肯定不符合要求) for(b=1;b=9;b+) /百位上的数码由1变到9 for(s=0;s=9;s+) /十位上的数码由1变到9 for(g=0;g=9;g+) /个位上的数码由1变到9 if(g+s+b=11)printf(%d ,g+s*10+b*100);num+; printf(ntotal: %dn,num); return 0;/*/方法二:移位求余得数码#include int mai
8、n( ) int g,s,b;/分别代表个位、十位、百位上的数码 int num=0,x,y; /x代表一个3位整数 for(x=100;x=999;x+) y=x; g=y%10; s=(y=y/10)%10; b=(y=y/10)%10; if(g+s+b=11) printf(%d ,x); num+; printf(ntotal: %dn,num); return 0;3-9/方法1三重循环#include int main( ) int d10,d5,d1; /分别代表10元、5元和1元纸币的数量 int num=0; for(d10=1;d10=9;d10+)for(d5=1;d5
9、=19;d5+) for(d1=1;d1=48;d1+) if(d1+d5+d10=50)&(10*d10+5*d5+d1=100) num+; printf( 1元纸币数:%dn,d1); printf( 5元纸币数:%dn,d5); printf(10元纸币数:%dn,d10); printf(n共有%d种兑换方案n,num); return 0;/*/方法2二重循环#include int main( ) int d10,d5,d1; /分别代表10元、5元和1元纸币的数量 int num=0; for(d10=1;d10=9;d10+)for(d5=1;d5=19;d5+) d1=50
10、-d10-d5; if(d1!=0 & (10*d10+5*d5+d1=100) num+;printf( 1元纸币数:%dn,d1);printf( 5元纸币数:%dn,d5);printf(10元纸币数:%dn,d10); printf(n共有%d种兑换方案n,num); return 0;*/3-10#include int main()int m,s,i;for (m=2;m1000;m+) s=0; /s用于存储因子之和for (i=1;im;i+)if (m%i)=0) s=s+i;if (s=m)printf(n%d is a 完数n,m);printf(its factors
11、are:);for (i=1;im;i+)if (m%i=0) printf(%d ,i);printf(n); return 0;3-11#include int main()int min,i;for(i=1;i=0;i+); /空循环min=i;printf(min: %dn,min);printf(max: %dn,-(min+1);return 0;3-12/方法1#include int main()/用n记录鞭炮声的次数,用t记时(秒),每秒监测鞭炮声/a1,b1,c1统计每人已放鞭炮数,a2,b2,c2记录当前放鞭炮的状态(值1表示放鞭炮状态)int t=1,n=1,num;i
12、nt a1=1,b1=1,c1=1,a2,b2,c2;printf(请输入每人的鞭炮数:);scanf(%d,&num);while(c1num)if(t%5=0&a1num) a2=1,a1+;else a2=0;if(t%6=0&b1num) b2=1,b1+;else b2=0;if(t%7=0&c1num) c2=1,c1+;else c2=0;if(a2=1|b2=1|c2=1) n+;t+;printf(一共能听到%d次鞭炮声n,n);return 0;/*/方法2分时间段统计#include int main()/用n记录鞭炮声的次数,用t记时(秒),每秒监测鞭炮声int t,n
13、=1,num;printf(请输入每人的鞭炮数:);scanf(%d,&num);for(t=1;t=(num-1)*5;t+) if(t%5=0|t%6=0|t%7=0) n+;for(;t=(num-1)*6;t+) if(t%6=0|t%7=0) n+;for(;t=(num-1)*7;t+) if(t%7=0) n+;printf(一共能听到%d次鞭炮声n,n);return 0;*/4-1#include int main()int hcf(int,int); /求最大公约数的函数int lcd(int,int,int); /求最小公倍数的函数int u,v,h,l;printf(请
14、输入两个整数:);scanf(%d%d,&u,&v);h=hcf(u,v);l=lcd(u,v,h);printf(它们的最大公约数是%dn,h);printf(它们的最小公倍数是%dn,l);return 0;int hcf(int u,int v)int t,r;if (vu) t=u,u=v,v=t;while(r=u%v)!=0) u=v,v=r;/辗转相除法return v;int lcd(int u,int v,int h) return(u*v/h); 4-2#include const double PI=3.14159;double girth(double r) /计算圆周
15、长return r*2*PI;double area(double r) /计算圆面积return r*r*PI;int main() double r; printf(请输入半径(r):); scanf(%lf,&r); printf(圆周长:%fn,girth(r); printf(圆面积:%fn,area(r); return 0;4-3#include int intcat(int x,int y)int n=10,z=y;while(y=y/10) n=n*10;z=x*n+z;return z;int main()int x,y;printf(Enter x,y: );scanf(
16、%d%d,&x,&y);printf(The new number is %dn,intcat(x,y);return 0;4-4#include int main()double getPower(int x,int y); /函数声明int x,y;printf(Enter x,y: );scanf(%d%d,&x,&y);printf(%d%d=%fn,x,y,getPower(x,y);return 0;double getPower(int x,int y)double f;if(y=1) f=x;else f=x*getPower(x,y-1);return f;4-5#inclu
17、de int main()int reverse(int n); /函数声明int n;printf(Enter n: );scanf(%d,&n);printf(The new number is %dn,reverse(n);return 0;int reverse(int n)int sign=1,k=0;if(n0) n=-n,sign=-1;dok=k*10+n%10;n/=10;while(n);return sign*k;4-6#include #include int main()float TriangleArea(float a, float b, float c); /函
18、数声明float a,b,c,area;printf(输入三角形三边a,b,c: );scanf(%f%f%f,&a,&b,&c);area=TriangleArea(a,b,c);if(area=-1)printf(%f,%f,%f)不能构成三角形!n,a,b,c);elseprintf(三角形(%f,%f,%f)面积为:%fn,a,b,c,area);return 0;float TriangleArea(float a, float b, float c)float s;if(a+b=c)|(a+c=b)|(b+c=a) return -1;s=(a+b+c)/2;return sqrt
19、(s*(s-a)*(s-b)*(s-c);4-7#include #include int main ()double fac(int); /函数声明double s=1.0, u=1.0;int x,n=1;printf(Enter x: );scanf(%d,&x);while(u=1.0e-6)u = pow(x,n)/fac(n); /调用系统函数和自定义函数s = s+u;n = n+1;printf(e%d=%fn,x,s);return 0; /求n!的函数(递推算法)double fac(int n)double f=1;if(n0)for(;n1;n-) f=f*n;/求n!
20、else if(n=0) f=1;/0!的值为1else if(n0) f=-1;/如果n为负数,则返回-1,表示错误return f;5-1#include #define M 3#define N 4/*/方法一int main()int aMN=1,2,3,4,5,6,7,8,9,10,11,12;int sum=0,i,j;for(i=0;iM;i+)for(j=0;jN;j+)sum=sum+aij;printf(arr_sum=%dn,sum);return 0;*/方法二int main()int arr_sum(int arrMN);/函数声明int aMN=1,2,3,4,5
21、,6,7,8,9,10,11,12;printf(arr_sum=%dn,arr_sum(a);return 0;int arr_sum(int arrMN)int sum=0,i,j;for(i=0;iM;i+)for(j=0;jN;j+)sum=sum+arrij;return sum;5-2#include #define N 3int main()int arr_xsum(int arrNN); /函数声明int aNN=1,2,3,4,5,6,7,8,9;int i,j;/输出数组元素for(i=0;iN;i+)for(j=0;jN;j+) printf(%3d ,aij);prin
22、tf(n);printf(narr_xsum=%dn,arr_xsum(a);return 0;int arr_xsum(int arrNN)int i,sum=0;for(i=0;iN;i+)sum=sum+arrii+arriN-1-i;if(N%2)sum=sum-arr(N-1)/2(N-1)/2; /若为奇数项数组return sum;5-3#include #define N 3int main()void convert_array(int arrNN); /函数声明int aNN=1,2,3,4,5,6,7,8,9;int i,j;printf(nThe original ar
23、ray:n);for(i=0;iN;i+)for(j=0;jN;j+) printf(%5d,aij);printf(n);convert_array(a);/函数调用printf(nThe converted array:n);for(i=0;iN;i+)for(j=0;jN;j+) printf(%5d,aij);printf(n);return 0;void convert_array(int arrNN)int i,j,t;for(i=0;iN;i+)for(j=i+1;jN;j+)t=arrij;arrij=arrji;arrji=t;5-4#include int main()in
24、t binary(int v,int n,int x);/函数声明(折半查找函数)int i,v10,x,find=-1;printf(按由小到大的顺序输入10个整数: n);for(i=0;i10;i+)scanf(%d,&vi);printf(Enter x to look for: );scanf(%d,&x);find=binary(v,10,x);/折半查找if(find!=-1)printf(%d been found. Its position is %dn,x,find+1);elseprintf(无此数n);return 0;/用折半查找法,在数组v(长度为n)中查找xint
25、 binary(int v,int n,int x) int low,high,mid,find=-1; /find=-1表示未找到 low=0;high=n-1; while(low=high) mid=(low+high)/2; if(xvmid) low=mid+1;else find=mid; break; return find;5-5#include #define N 80int main()char strN,x;void del_char(char str,char x);/函数声明printf(Enter a string: );gets(str);printf(Enter
26、 the character deleted: );scanf(%c,&x);del_char(str,x);/函数调用printf(The new string is: %sn,str);return 0;/在一行字符串中删去指定的字符(x)将删除所有指定的字符void del_char(char str,char x)int i,j;for(i=0,j=0;stri!=0;i+)if(stri!=x) strj+=stri;strj=0;5-6#include #include int main()char str80;void reverse(char str);printf(请输入字符
27、串:);gets(str);reverse(str);printf(反转后的字符串为:);puts(str);return 0;/字符串反转void reverse(char str)char t;int i,j,len=strlen(str);for(i=0,j=len;ilen/2;i+,j-)t=stri;stri=strj-1;strj-1=t;5-7#include #include #define N 5int main() int i,j,n=N; char strN80,temp80; printf(please input strings:n); for(i=0;in;i+)
28、 gets(stri); /起泡法排序 for(i=0;in-1;i+) for(j=0;j0) strcpy(temp,strj);strcpy(strj,strj+1);strcpy(strj+1,temp); printf(nsorted strings:n); for(i=0;in;i+) puts(stri); return 0;5-8#include int main()int i,j,upper,lower,digit,space,other;char text380;upper=lower=digit=space=other=0;for(i=0;i3;i+)printf(ple
29、ase input line %dn,i+1);gets(texti);for(j=0;j=A& textij=a & textij=0 & textij=9)digit+;else if (textij= )space+;elseother+;printf(upper case:%dn,upper);printf(lower case:%dn,lower);printf(digit :%dn,digit);printf(space :%dn,space);printf(other :%dn,other);return 0;6-1#include #define N 3int main()vo
30、id convert_array(int arr,int n); /函数声明int aNN=1,2,3,4,5,6,7,8,9;int i,j,n=N;printf(nThe original array:n);for(i=0;in;i+)for(j=0;jn;j+) printf(%5d,aij);printf(n);convert_array(a0,n);printf(nThe converted array:n);for(i=0;in;i+)for(j=0;jn;j+) printf(%5d,aij);printf(n);return 0;void convert_array(int a
31、rr,int n) /形参用指针(int *arr)或一维数组int i,j,t;for(i=0;in;i+)for(j=i+1;jn;j+)t=arri*n+j;arri*n+j=arrj*n+i;arrj*n+i=t;6-2#include int main()int arr_edge_sum(int *arr,int m,int n); /函数声明int a34=1,2,3,4,5,6,7,8,9;int i,j;printf(The array data:n);for(i=0;i3;i+)for(j=0;j4;j+) printf(%5d,aij);printf(n);printf(n
32、sum=%dn,arr_edge_sum(a0,3,4);return 0;int arr_edge_sum(int *arr,int m,int n)/形参用指针或一维数组int i,j,sum=0;for(i=0,j=0;jn;j+)sum=sum+arri*n+j;for(i=m-1,j=0;jn;j+)sum=sum+arri*n+j;for(i=1,j=0;im-1;i+)sum=sum+arri*n+j;for(i=1,j=n-1;im-1;i+)sum=sum+arri*n+j;return(sum);6-3#include int main() void bubble(int,
33、int,int); /函数声明 int a34=1,2,-3,4,9,10,11,-12,5,6,7,-8; int i,j; printf(The original array:n); for(i=0;i3;i+) for(j=0;j4;j+) printf(%5d,aij); printf(n); bubble(a0,3,4); /数组排序 printf(The sorted array:n); for(i=0;i3;i+) for(j=0;j4;j+) printf(%5d,aij);printf(n); return 0;/起泡法排序void bubble(int array,int
34、m,int n) int i,j,k,t; for(i=1;im*n;i+) k=0; for(j=0;jarrayj+1) t=arrayj; arrayj=arrayj+1; arrayj+1=t;k+; if(k=0) break; /若本轮没有交换操作,提前退出 6-4#include #include int main()char str80;int strToInt(char *str); /函数声明printf(请输入数字串:);gets(str);printf(数字串转换成整数后再加1000的结果:%dn,strToInt(str)+1000);return 0;int str
35、ToInt(char *str)int i,n=*str-0,len=strlen(str);for(i=1;ilen;i+)n=n*10+*(str+i)-0;return n;6-5#include #define N 80int main()char strN,x;void del_char(char *str,char x);/函数声明printf(Enter a string: );gets(str);printf(Enter the character deleted: );scanf(%c,&x);del_char(str,x);/函数调用printf(The new string is: %sn,str);return 0;/在一行字符串中删去指定的字符(x)将删除所有指定的字符void del_char(char *p,char x)char *q=p;for(;*p!=0;p+)if(*p!=x)*q+=*p;*q=0;/*/第二种方法只能删除字符串中找到的第一个,但更具有独立性和灵活性/先确定被删字符的位置(p指针),/然