用选择法对10个整数从小到大排序。
#includeP12. C语言程序设计教程(第三版)课后习题7.3int main() { int i,j,t,a[10]; for(i=0;i<10;i++) { scanf("%d",&a[i]); } for(i=0;i<9;i++) { for(j=i+1;j<10;j++) { if(a[j]
求一个3×3矩阵对角线元素之和。
#includeP13. C语言程序设计教程(第三版)课后习题7.5int main() { int a[3][3]; int sum1=0,sum2=0; int i,j; for(i=0;i<3;i++){ for(j=0;j<3;j++){ scanf("%d", &a[i][j]); if(i==j){ sum1+=a[i][j]; } } } j=2; for(i=0;i<=2;i++){ sum2+=a[i][j]; j--; } printf("%d %d", sum1, sum2); return 0; }
输入10个数字,然后逆序输出。
#includeP14. C语言程序设计教程(第三版)课后习题8.1int main() { int a[10]; int i,m,n; for(i=0;i<10;i++){ scanf("%d", &a[i]); } i--; for(i;i>-1;i--){ printf("%dn", a[i]); } return 0; }
写两个函数hcf和lcd,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果两个整数由键盘输入。
#includeP15. C语言程序设计教程(第三版)课后习题8.2int hc(int a,int b) { if(a==b) { return a; } else if(a>b) { return hc(a-b,b); } else { return hc(b-a,a); } } int lcd(int a,int b) { int i=1; while((a*i)%b!=0) { i++; } return i*a; } int main() { int a,b; scanf("%d %d",&a,&b); int i=hc(a,b); int j=lcd(a,b); printf("%d %d",i,j); return 0; }
求方程ax^2+bx+c=0的根,用三个函数gz,ez和sz分别求当b^2-4ac大于0、等于0和小于0时的根,并输出结果。从主函数输入a、b、c的值。(输出保留3位小数)
#includeP16. C语言程序设计教程(第三版)课后习题8.3#include float x1, x2, disc, p, q; void gz(float a, float b) { float m = sqrt(disc); x1 = (-b + sqrt(disc)) / (2 * a); x2 = (-b - sqrt(disc)) / (2 * a); } void ez(float a, float b) { x1 = x2 = (-b) / (2 * a); } void sz(float a, float b) { p = -b / (2 * a); q = sqrt(-disc) / (2 * a); } int main() { int a, b, c; scanf("%d %d %d", &a, &b, &c); disc = b*b - 4 * a*c; if (disc > 0) { gz(a, b); printf("x1=%.3f x2=%.3fn", x1, x2); } else if (disc == 0) { ez(a, b); printf("x1=%.3f x2=%.3fn", x1, x2); } else { sz(a, b); printf("x1=%.3f+%.3fi x2=%.3f-%.3fin", p, q, p, q); } return 0; }
写一个判断素数的函数prim,在主函数输入一个整数,输出是否是素数的消息。
#includeP17. C语言程序设计教程(第三版)课后习题8.4int prime(int a); int main() { int n,i; scanf("%d",&n); if(prime(n)==1) printf("prime"); else printf("not prime") ; } int prime(int a) { int i; for(i=2; i
写一个函数convert,使给定的一个二维数组(3×3)转置,即行列互换。
#includeP18. C语言程序设计教程(第三版)课后习题8.5int main() { int a[3][3],i,j; for(i=0; i<3; i++){ for(j=0; j<3; j++){ scanf("%d",&a[i][j]); } } for(i=0; i<3; i++){ for(j=0; j<3; j++){ printf("%d ",a[j][i]); } printf("n"); } return 0; }
写一函数inverse,使输入的一个字符串按反序存放,在主函数中输入输出反序后的字符串。
#includeP19. C语言程序设计教程(第三版)课后习题8.6#include void inverse(char a[]) { for(int i=strlen(a)-1;i>=0;i--) { printf("%c",a[i]); } } int main() { char a[50]; scanf("%s",&a); inverse(a); return 0; }
写一函数concat,将两个字符串连接
#includeP20. C语言程序设计教程(第三版)课后习题8.7#include int main() { char str1[100],str2[100]; gets(str1); gets(str2); strcat(str1,str2); puts(str1); return 0; }
写一函数cpy,将一个小写字符串中的所有字母复制到另一个字符串中,然后按照字典序输出。
P21. C语言程序设计教程(第三版)课后习题8.8写一函数insert,输入一个四位数字,要求输出这四个数字字符,但每两个数字间空格。如输入1990,应输出"1 9 9 0"。
#include#include int main() { char a[10]; gets(a); for(int i=0; i P22. C语言程序设计教程(第三版)课后习题8.9 编写一函数count,由实参传来一个字符串,统计此字符串中数字、字母、空格和其它字符的个数,在主函数中输入字符串以及输出上述结果。 只要结果,别输出什么提示信息。
P23. C语言程序设计教程(第三版)课后习题9.2输入两个整数,求他们相除的余数。用带参的宏来实现,编程序。
#includeP24. C语言程序设计教程(第三版)课后习题9.3#define s(a,b) a%b int main() { int x,y; scanf("%d%d",&x,&y); printf("%d",s(x,y)); return 0; } 三角形面积=SQRT(S*(S-a)*(S-b)*(S-c)) 其中S=(a+b+c)/2,a、b、c为三角形的三边。定义两个带参的宏,一个用来求area,另一个宏用来求S。 写程序,在程序中用带实参的宏名来求面积area。
#includeP25. C语言程序设计教程(第三版)课后习题9.4#include #define s(a,b,c) (a+b+c)/2 #define area(a,b,c,s) sqrt(s*(s-a)*(s-b)*(s-c)) int main() { float x,y,z; scanf("%f%f%f",&x,&y,&z); printf("%.3f",area(x,y,z,s(x,y,z)));//先算小括号,然后大括号; return 0; } 给年份year,定义一个宏,以判别该年份是否闰年。提示:宏名可以定义为LEAP_YEAR,形参为y,即定义宏的形式为 #define LEAP_YEAR(y) (读者设计的字符串)
#includeP26. C语言程序设计教程(第三版)课后习题9.6#define LEAP_YEAR(y) (y%4==0 && y%100!=0) || (y%400==0)?'L':'N' int main() { int y; scanf("%d",&y); int l=LEAP_YEAR(y); printf("%c",l); return 0; } ⑴第一行输出一个实数;⑵第二行内输出两个实数;⑶第n行输出n个实数。实数用".3f"格式输出
#includeP27. C语言程序设计教程(第三版)课后习题9.8#define pr1(x) printf("%.3fn",x) #define pr2(x) printf("%.3f,%.3fn",x,x) #define pr3(x) printf("%.3f,%.3f,%.3fn",x,x,x) #define pr4(x) printf("%.3f,%.3f,%.3f,%.3fn",x,x,x,x) #define pr5(x) printf("%.3f,%.3f,%.3f,%.3f,%.3fn",x,x,x,x,x) int main() { float x; int m; scanf("%f%d", &x,&m); if (m == 1) { pr1(x); } else if (m == 2) { pr1(x); pr2(x); } else if (m == 3) { pr1(x); pr2(x); pr3(x); } else if (m == 4) { pr1(x); pr2(x); pr3(x); pr4(x); } else if (m == 5) { pr1(x); pr2(x); pr3(x); pr4(x); pr5(x); } return 0; } 分别用函数和带参的宏,从三个数中找出最大的数。
#includeP28. C语言程序设计教程(第三版)课后习题9.10#define max(a,b,c) a>=(b>=c?b:c)?a:(b>=c?b:c) int main() { float m(float a,float b,float c); float t1,t2,a,b,c; scanf("%f %f %f",&a,&b,&c); t1=max(a,b,c); printf("%.3fn",t1); t2=m(a,b,c); printf("%.3f",t2); return 0; } float m(float a,float b,float c) { float x; x=(a>=(b>=c?b:c)?a:(b>=c?b:c)); return (x); } 输入一行电报文字,将字母变成其下一字母(如’a’变成’b’……’z’变成’a’其它字符不变)。
#includeP29. C语言程序设计教程(第三版)课后习题10.1#include int main() { char a[50]; gets(a); for(int i=0; i ='a'&&a[i]<'z'||a[i]>='A'&&a[i]<'Z') { a[i]=a[i]+1; } else if(a[i]=='z'||a[i]=='Z') { a[i]+=1; } else a[i]=a[i]; } puts(a); return 0; } 输入三个整数,按由小到大的顺序输出。使用指针
#includeP30. C语言程序设计教程(第三版)课后习题10.2int main() { int a,b,c,t=0; scanf("%d%d%d",&a,&b,&c); if(a>b) { t=a; a=b; b=t; } if(a>c) { t=a; a=c; c=t; } if(b>c) { t=b; b=c; c=t; } printf("%d %d %d ",a,b,c); return 0; } 输入三个字符串,按由小到大的顺序输出
# includeP31. C语言程序设计教程(第三版)课后习题10.3# include int main() { char ch1[1000],ch2[1000],ch3[1000],ch4[1000]; int m,n,i,j,k; gets(ch1);gets(ch2);gets(ch3); if(strcmp(ch1,ch2)>0) { strcpy(ch4,ch2); strcpy(ch2,ch1); strcpy(ch1,ch4); } if(strcmp(ch1,ch3)>0) { strcpy(ch4,ch3); strcpy(ch3,ch1); strcpy(ch1,ch4); } if(strcmp(ch2,ch3)>0) { strcpy(ch4,ch3); strcpy(ch3,ch2); strcpy(ch2,ch4); } puts(ch1); puts(ch2); puts(ch3); return 0; } 输入10个整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换。写三个函数; ①输入10个数;②进行处理;③输出10个数。使用指针
#includeP32. C语言程序设计教程(第三版)课后习题10.4int main() { void change(int *x); int a[10],i,j; for(i=0; i<10; i++) scanf("%d",&a[i]); change(a); for(i=0; i<10; i++) printf("%d ",a[i]); } void change(int *a) { int *min,*max,*p,t; max=min=a; for(p=a+1; p*max) max=p; t=a[9]; a[9]=*max; *max=t; for(p=a+1; p 有10个整数,使前面各数顺序向后移m个位置,最后m个数变成前面m个数。写一函数move:实现以上功能,在主函数中输入10个数和输出调整后的10个数。
P33. C语言程序设计教程(第三版)课后习题10.5有n人围成一圈,顺序排号。从第1个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来的第几号的那位。使用指针
#includeint main() { int a[50], i, n, count, flag=0; scanf("%d", &n); count=n; for (i=1; i<=n; i++) a[i]=1; for (i=1; ; i++) { if (i==n+1) i=1; if (a[i]!=0) flag++; else continue; if (flag%3==0) { a[i]=0; count--; } if (count==1) break; } for (i=1; i<=n; i++) if (a[i]!=0) printf("%d", i); return 0; }



