目录
- PTA 浙大版《C语言程序设计(第4版)》题目集 参考答案(编程题)
- 本答案配套详解教程专栏
- 练习2-1 Programming in C is fun! (5 分)
- 练习2-3 输出倒三角图案 (5 分)
- 练习2-4 温度转换 (5 分)
- 练习2-6 计算物体自由下落的距离 (5 分)
- 练习2-8 计算摄氏温度 (10 分)
- 练习2-9 整数四则运算 (10 分)
- 练习2-10 计算分段函数[1] (10 分)
- 练习2-11 计算分段函数[2] (10 分)
- 练习2-12 输出华氏-摄氏温度转换表 (15 分)
- 练习2-13 求N分之一序列前N项和 (15 分)
- 练习2-14 求奇数分之一序列前N项和 (15 分)
- 练习2-15 求简单交错序列前N项和 (15 分)
- 练习2-17 生成3的乘方表 (15 分)
- 练习2-18 求组合数 (15 分)
- 习题2-1 求整数均值 (10 分)
- 习题2-2 阶梯电价 (15 分)
- 习题2-3 求平方与倒数序列的部分和 (15 分)
- 习题2-4 求交错序列前N项和 (15 分)
- 习题2-5 求平方根序列前N项和 (15 分)
- 习题2-6 求阶乘序列前N项和 (15 分)
- 练习3-2 计算符号函数的值 (10 分)
- 练习3-3 统计学生平均成绩与及格人数 (15 分)
- 练习3-4 统计字符 (15 分)
- 练习3-5 输出闰年 (15 分)
- 练习3-7 成绩转换 (15 分)
- 练习3-8 查询水果价格 (15 分)
- 习题3-1 比较大小 (10 分)
- 习题3-2 高速公路超速处罚 (15 分)
- 习题3-3 出租车计价 (15 分)
- 习题3-4 统计学生成绩 (15 分)
- 习题3-5 三角形判断 (15 分)
- 练习4-3 求给定精度的简单交错序列部分和 (15 分)
- 练习4-6 猜数字游戏 (15 分)
- 练习4-7 求e的近似值 (15 分)
- 练习4-10 找出最小值 (20 分)
- 练习4-11 统计素数并求和 (20 分)
- 习题4-1 求奇数和 (15 分)
- 习题4-2 求幂级数展开的部分和 (20 分)
- 习题4-3 求分数序列前N项和 (15 分)
- 习题4-4 特殊a串数列求和 (20 分)
- 习题4-5 换硬币 (20 分)
- 习题4-6 水仙花数 (20 分)
- 习题4-7 最大公约数和最小公倍数 (15 分)
- 习题4-8 高空坠球 (20 分)
- 习题4-9 打印菱形图案 (15 分)
- 习题4-10 猴子吃桃问题 (15 分)
- 习题4-11 兔子繁衍问题 (15 分)
- 练习6-3 英文字母替换加密(大小写转换+后移1位) (15 分)
- 习题6-7 简单计算器 (20 分)
- 习题6-8 单词首字母大写 (15 分)
- 练习7-2 求最大值及其下标 (20 分)
- 练习7-3 将数组中的数逆序存放 (20 分)
- 练习7-4 找出不是两个数组共有的元素 (20 分)
- 练习7-7 矩阵运算 (20 分)
- 练习7-8 方阵循环右移 (20 分)
- 练习7-9 计算天数 (15 分)
- 练习7-10 查找指定字符 (15 分)
- 练习7-11 字符串逆序 (15 分)
- 习题7-1 选择法排序 (20 分)
- 习题7-2 求一批整数中出现最多的个位数字 (20 分)
- 习题7-3 判断上三角矩阵 (15 分)
- 习题7-4 求矩阵各行元素之和 (15 分)
- 习题7-5 找鞍点 (20 分)
- 习题7-6 统计大写辅音字母 (15 分)
- 习题7-7 字符串替换 (15 分)
- 习题7-8 字符串转换成十进制整数 (15 分)
- 习题8-7 字符串排序 (20 分)
- 习题8-10 输出学生成绩 (20 分)
- 习题9-1 时间换算 (15 分)
- 习题9-3 平面向量加法 (15 分)
- 习题9-4 查找书籍 (20 分)
- 习题9-5 通讯录排序 (20 分)
PTA 浙大版《C语言程序设计(第4版)》题目集 参考答案(编程题)
本答案配套详解教程专栏
欢 迎 订 阅
PTA浙大版《C语言程序设计(第4版)》题目集 详解教程
练习2-1 Programming in C is fun! (5 分)
#includeint main(){printf("Programming in C is fun!");return 0;}
练习2-3 输出倒三角图案 (5 分)
#includeint main(){printf("* * * *\n");printf(" * * *\n");printf("* *\n");printf(" *\n");return 0;}
练习2-4 温度转换 (5 分)
#includeint main(){int C,F=150;C=5*(F-32)/9;printf("fahr = 150, celsius = %d",C);return 0;}
练习2-6 计算物体自由下落的距离 (5 分)
#include int main(){float h,t = 3,g = 10;h = 0.5*g*t*t;printf("height = %.2f",h);return 0;}
练习2-8 计算摄氏温度 (10 分)
#includeint main(void){int C,F;scanf("%d",&F);C=5*(F-32)/9;printf("Celsius = %d",C);return 0;}
练习2-9 整数四则运算 (10 分)
#includeint main(){int m,n;scanf("%d %d",&m,&n);printf("%d + %d = %d\n",m,n,m+n);printf("%d - %d = %d\n",m,n,m-n);printf("%d * %d = %d\n",m,n,m*n);printf("%d / %d = %d",m,n,m/n);return 0;}
练习2-10 计算分段函数[1] (10 分)
#includeint main(){float x,result;scanf("%f",&x);if(x!=0){result=1/x;}else {result=0;}printf("f(%.1f) = %.1f",x,result);return 0;}
练习2-11 计算分段函数[2] (10 分)
#include #include int main(){float x, y;scanf("%f", &x);if(x>=0)y=sqrt(x);else y=pow(x+1,2)+2*x+1/x;printf("f(%.2f) = %.2f", x,y);return 0;}
练习2-12 输出华氏-摄氏温度转换表 (15 分)
for循环
版本
#includeint main(){int lower,upper,F;float C;scanf("%d %d",&lower,&upper);F=lower;if(lower<=upper&&upper<=100){printf("fahr celsius\n");C=5.0*(F*1.0-32)/9.0;printf("%d%6.1f",F,C);//占据6个字符宽度,靠右对齐,保留1位小数for(F=lower+2;F<=upper;F+=2){C=5.0*(F*1.0-32)/9.0;printf("\n%d%6.1f",F,C);//保证结尾无空行}}else printf("Invalid.");return 0;}
while循环
版本
#includeint main(){int lower,upper,F;float C;scanf("%d %d",&lower,&upper);F=lower;if(lower<=upper&&upper<=100){printf("fahr celsius\n");C=5.0*(F*1.0-32)/9.0;printf("%d%6.1f",F,C);//占据6个字符宽度,靠右对齐,保留1位小数while((F+=2)<=upper){C=5.0*(F*1.0-32)/9.0;printf("\n%d%6.1f",F,C);//保证结尾无空行}}else printf("Invalid.");return 0;}
do-while循环
版本
#includeint main(){int lower,upper,F;float C;scanf("%d %d",&lower,&upper);F=lower;if(lower<=upper&&upper<=100){printf("fahr celsius\n");do{C=5.0*(F*1.0-32)/9.0;printf("%d%6.1f\n",F,C);//占据6个字符宽度,靠右对齐,保留1位小数}while((F+=2)<=upper);}else printf("Invalid.");return 0;}
练习2-13 求N分之一序列前N项和 (15 分)
#includeint main(){double sum=0;int n,i;scanf("%d",&n);for(i=1;i<=n;i++){sum=sum+1.0/i;}printf("sum = %.6f\n",sum);return 0;}
练习2-14 求奇数分之一序列前N项和 (15 分)
#includeint main(void){double sum=0;int n,i;scanf("%d",&n);for(i=1;i<=n;i++){sum=sum+1.0/(2*i-1);}printf("sum = %.6lf\n",sum);return 0;}
练习2-15 求简单交错序列前N项和 (15 分)
#includeint main(){double sum=0;int i,n,flag=1;scanf("%d",&n);for(i=1;i<=n;i++){sum+=1.0/(3*i-2)*(flag);flag*=-1;}printf("sum = %.3lf",sum);return 0;}
练习2-17 生成3的乘方表 (15 分)
#include#includeint main(){int i,n;scanf("%d",&n);for(i=0;i<=n;i++){printf("pow(3,%d) = %.0f\n",i,pow(3,i));}return 0;}
练习2-18 求组合数 (15 分)
#includedouble fact(int n){double fact=1;for(int i=1;i<=n;i++){fact*=i;}return fact;}int main(){double result;double m,n;scanf("%lf %lf",&m,&n);result = fact(n)/(fact(m)*fact(n-m));printf("result = %.0f",result);return 0;}
习题2-1 求整数均值 (10 分)
#includeint main(){int a,b,c,d,sum;float average;scanf("%d %d %d %d",&a,&b,&c,&d);sum=a+b+c+d;average=sum/4.0;printf("Sum = %d; Average = %.1f",sum,average);return 0;}
习题2-2 阶梯电价 (15 分)
#includeint main(){int n;double cost;scanf("%d",&n);if(n<0){printf("Invalid Value!");return 0;}else if(n<=50)cost=0.53*n;else cost=50*0.53+0.58*(n-50);printf("cost = %.2f",cost);return 0;}
习题2-3 求平方与倒数序列的部分和 (15 分)
#includeint main(){int m,n;double sum=0;scanf("%d %d",&m,&n);for(int i=m;i<=n;i++)sum+=i*i+1.0/i;printf("sum = %.6f",sum);return 0;}
习题2-4 求交错序列前N项和 (15 分)
#includeint main(){double i,item,sum=0;int n,flag=1;scanf("%d",&n);for(i=1;i<=n;i++){item=flag*i/(2*i-1);sum+=item;flag*=-1;}printf("%.3f",sum);return 0;}
习题2-5 求平方根序列前N项和 (15 分)
#include#includeint main(){int n,i;double sum=0,item;scanf("%d",&n);for(i=1;i<=n;i++){item=sqrt(i);sum+=item;}printf("sum = %.2f",sum);return 0;}
习题2-6 求阶乘序列前N项和 (15 分)
#includeint main(){int n,sum=0,i,j=1;scanf("%d",&n);for(i=1;i<=n;i++) {j*=i;sum+=j;}printf("%d",sum);}
练习3-2 计算符号函数的值 (10 分)
#includeint main(){int n,sign;scanf("%d",&n);if(n<0)sign=-1;if(n==0)sign=0;if(n>0)sign=1;printf("sign(%d) = %d",n,sign);return 0;}
练习3-3 统计学生平均成绩与及格人数 (15 分)
#includeint main(void){int i,N,score,count=0;double sum=0,average;scanf("%d",&N);if(N==0){printf("average = 0.0\n");printf("count = 0");}else{for(i=1;i<=N;i++){scanf("%d",&score);sum=sum+score;if(score>=60)count++;}average=sum/N;printf("average = %.1f\n",average);printf("count = %d",count);}return 0;}
练习3-4 统计字符 (15 分)
#include int main(){int i,letter = 0, blank = 0, digit = 0, other = 0;char ch;for(i=0;i<=9;i++){ch=getchar();if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))letter++;else if((ch==' ')||(ch=='\n'))blank++;else if(ch>='0'&&ch<='9')digit++;else other++;}printf("letter = %d, blank = %d, digit = %d, other = %d",letter,blank,digit,other);return 0;}
练习3-5 输出闰年 (15 分)
#includeint main(){int i,j=0,year;scanf("%d",&year);if(year<=2000||year>2100)printf("Invalid year!");else{for(i=2001;i<=year;i++){if((i%4==0&&i%100!=0)||(i%400==0)){printf("%d\n",i);j=1;}}if(j==0)printf("None");}return 0;}
练习3-7 成绩转换 (15 分)
#include int main(){int x;scanf("%d",&x);switch(x/10){case 10:printf("A");break;case 9:printf("A"); break;case 8:printf("B"); break;case 7:printf("C"); break;case 6:printf("D"); break;default:printf("E");}return 0;}
练习3-8 查询水果价格 (15 分)
#includeint main(void){int i,a;printf("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit\n"); for(i=1;i<=5;i++){ scanf("%d",&a);if(a==0)break;switch(a){case 1:printf("price = 3.00\n");break;case 2:printf("price = 2.50\n");break;case 3:printf("price = 4.10\n");break;case 4:printf("price = 10.20\n");break;default:printf("price = 0.00\n");}}return 0;}
习题3-1 比较大小 (10 分)
#includeint main(){int x,y,z,t;scanf("%d %d %d",&x,&y,&z);if(x>y){t=y;y=x;x=t;}if(x>z){t=z;z=x;x=t;}if(y>z){t=z;z=y;y=t;}printf("%d->%d->%d\n",x,y,z);return 0;}
习题3-2 高速公路超速处罚 (15 分)
#includeint main(){int v,limit;double exceed;scanf("%d %d",&v,&limit);exceed=(v-limit)*100.0/limit;if(exceed>=50)printf("Exceed %.f%%. License Revoked",exceed);else if(exceed>=10)printf("Exceed %.f%%. Ticket 200",exceed);else printf("OK");return 0;}
习题3-3 出租车计价 (15 分)
#includeint main(){double mileage;int time,pay;scanf("%lf %d",&mileage,&time);if(mileage<=3)pay=10;else if(mileage<=10)pay=10+2*(mileage-3)+0.5;else pay=24+(mileage-10)*3+0.5;pay+=time/5*2;printf("%d", pay);return 0;}
习题3-4 统计学生成绩 (15 分)
#includeint main(){int n,i,grade,a=0,b=0,c=0,d=0,e=0;scanf("%d",&n); for(i=1;i<=n;i++){scanf("%d",&grade);switch (grade/10){case 10: case 9:a++;break;case 8:b++;break;case 7:c++;break;case 6:d++;break;default:e++;break;}}printf("%d %d %d %d %d",a,b,c,d,e);return 0;}
习题3-5 三角形判断 (15 分)
#include#includeint main(){double x1,y1,x2,y2,x3,y3,p,s;double j,k,l;scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3);j=sqrt(pow(x1-x2,2)+pow(y1-y2,2));k=sqrt(pow(x2-x3,2)+pow(y2-y3,2));l=sqrt(pow(x1-x3,2)+pow(y1-y3,2));if(j+k>l&&k+l>j&&j+l>k){p=(j+k+l)/2;s=sqrt(p*(p-j)*(p-k)*(p-l));//海伦公式printf("L = %.2lf, A = %.2lf",p*2,s);}else printf("Impossible");return 0;}
练习4-3 求给定精度的简单交错序列部分和 (15 分)
#includeint main (){double sum=0,eps,item;int n=1,flag=1;scanf("%lf",&eps);do{item=1.0/(3*n-2);sum+=flag*item;flag=-flag;n++;}while(item>eps);printf("sum = %f",sum);return 0;}
练习4-6 猜数字游戏 (15 分)
while循环
#include int main(){int random,i=1,n,input;scanf("%d %d",&random,&n);scanf("%d",&input);while(i<=n&&input>=0){if(i==1&&input==random){printf("Bingo!\n");break;}if((i==2||i==3)&&input==random){printf("Lucky You!\n");break;}if(i>3&&input==random){printf("Good Guess!\n");break;}if(input>random)printf("Too big\n");if(input<random)printf("Too small\n");scanf("%d",&input);i++;}if (i==n+1||input<0)printf("Game Over");return 0;}
for循环
#includeint main (){int random,n,i,a;scanf("%d %d",&random,&n);for(i=1;i<=n;i++){scanf("%d",&a);if(a<0){printf("Game Over\n");break;}if(a==random){if(i==1){printf("Bingo!");break;}else if(i<=3){printf("Lucky You!");break;}else if(i<=n){printf("Good Guess!");break;}}if(a>random)printf("Too big\n");if(a<random)printf("Too small\n");if(i==n)printf("Game Over\n");}return 0;}
练习4-7 求e的近似值 (15 分)
#includeint main(){double e=1,fact=1;int n;scanf("%d",&n);for(int i=1;i<=n;i++){fact*=i;e+=1/fact;}printf("%.8lf",e);return 0;}
练习4-10 找出最小值 (20 分)
#includeint main(){int min,n,temp;scanf("%d",&n);scanf("%d",&min);for(int i=1;i<n;i++){scanf("%d",&temp);if(temp<min)min=temp;}printf("min = %d",min);return 0;}
练习4-11 统计素数并求和 (20 分)
#includeint main(){int M,N,count=0,sum=0,i,j;scanf("%d %d",&M,&N);for(i=M;i<=N;i++){for(j=2;j<i;j++)if(i%j==0)break;if(i==j){count++;sum+=i;}}printf("%d %d",count,sum);return 0;}
习题4-1 求奇数和 (15 分)
#includeint main(){int num,sum=0;do{scanf("%d",&num);if(num>0&&num%2==1)sum+=num;}while(num>0);printf("%d",sum);return 0;}
习题4-2 求幂级数展开的部分和 (20 分)
#include #define eps 0.00001int main(){double x,sum=1,item = 1.0,n = 1.0;scanf("%lf",&x);while(item>=eps){item *= x/n;sum+=item;n++;}printf("%.4f",sum);return 0;}
习题4-3 求分数序列前N项和 (15 分)
#include int main(){int n,i;double a=2,b=1,temp,sum=0;scanf("%d", &n);for(i=1;i<=n;i++){sum+=a/b;temp=a;a+=b;b=temp;}printf("%.2f",sum);return 0;}
习题4-4 特殊a串数列求和 (20 分)
#includeint main(){int a,n,i,item=0,sum=0;scanf("%d %d",&a,&n);for(i=1;i<=n;i++){item=item*10+a;sum+=item;}printf("s = %d",sum);return 0;}
习题4-5 换硬币 (20 分)
# include int main(){int count=0,i,j,k;double x;if(scanf("%lf",&x)==1);for(i=x/5;i>=1;i--){for(j=(x-5*i)/2;j>=1;j--){for(k=x-5*i-2*j;k>=1;k--){if(5*i+2*j+k==x){printf("fen5:%d, fen2:%d, fen1:%d, total:%d\n",i,j,k,i+j+k);count++;}}}}printf("count = %d",count);return 0;}
习题4-6 水仙花数 (20 分)
#include int ipow( int x, int n);int main() {int N, i,j,temp,remainder,Npower, sum;scanf("%d", &N);for (i = ipow(10, N - 1); i < ipow(10, N ); i++) {sum = 0;temp = i;while (temp != 0) {Npower=1;remainder=temp % 10;Npower=ipow(remainder,N);sum += Npower;temp /= 10;}if (i == sum)printf("%d\n", i);}return 0;}int ipow( int x, int n){int i, p=1;for (i=0; i<n; i++) p *=x;return p;}
习题4-7 最大公约数和最小公倍数 (15 分)
#include int GCD( int M, int N);int main(){int M, N, gcd, lcm;scanf("%d %d", &M, &N);if (M>N) gcd = GCD(M, N);else gcd = GCD(N, M);lcm=M*N/gcd;printf("%d %d\n", gcd, lcm);return 0;}int GCD( int M, int N){int r = M%N;while (r) {M = N; N = r;r = M%N;}return N; }
习题4-8 高空坠球 (20 分)
#includeint main(){int i,n;double H,h,s;scanf("%lf %d",&H,&n);if(n==0)printf("0.0 0.0");else {s=H;h=H/2;for(i=2;i<=n;i++){s+=2*h;h/=2;}printf("%.1lf %.1lf",s,h);}return 0;}
习题4-9 打印菱形图案 (15 分)
#includeint main(){int n;scanf("%d",&n);for(int i=1;i<=n/2+1;i++){for(int j=(n/2+1-i)*2;j>=1;j--)printf(" ");for(int j=1;j<=2*i-1;j++)printf("* ");printf("\n");}for(int i=1;i<=n/2;i++){for(int j=1;j<=2*i;j++)printf(" ");for(int j=n-2*i;j>=1;j--)printf("* ");printf("\n");}return 0;}
习题4-10 猴子吃桃问题 (15 分)
#includeint main(){int n,i,sum=0,num=1;scanf("%d",&n);for(i=n-1;i>=1;i--){sum=(num+1)*2;num=sum;}printf("%d",sum);return 0;}
习题4-11 兔子繁衍问题 (15 分)
//斐波那契数列#includeint main(){int month,N,F1=1,F2=1,F3=0;//F1,F2,F3表示连续三个月的兔子数量scanf("%d",&N);if(N==1)month=1;else{month=2;while(F3<N){F3=F1+F2;F1=F2;F2=F3;month++;}}printf("%d",month);return 0;}
练习6-3 英文字母替换加密(大小写转换+后移1位) (15 分)
#includeint main(){char c,delta;delta='A'-'a';do{scanf("%c",&c);if(c>='a'&&c<'z')c+=(delta+1);else if(c>='A'&&c<'Z')c-=(delta-1);else if(c=='Z')c='a';else if(c=='z')c='A';printf("%c",c);}while(c!='\n');return 0;}
习题6-7 简单计算器 (20 分)
#includeint main(){int a,sum;char c;scanf("%d",&sum);while((c=getchar())!='='){scanf("%d",&a);switch(c){case '+':sum+=a;break;case '-':sum-=a;break;case '*':sum*=a;break;case '/':if(a==0){printf("ERROR");return 0;}else sum/=a;break;default:printf("ERROR");return 0;}}printf("%d",sum);return 0;}
习题6-8 单词首字母大写 (15 分)
#includeint main(){char ch,prev=' ';while((ch=getchar())!='\n'){if (ch>='a'&&ch<='z'&&prev==' ') ch=ch-'a'+'A';putchar(ch);prev=ch;}return 0;}
练习7-2 求最大值及其下标 (20 分)
#includeint main(){int n,i,max,index=0;scanf("%d",&n);int a[n];for(i=0;i<n;i++)scanf("%d",&a[i]);max=a[0];for(i=0;i<n;i++){if(a[i]>max){max=a[i];index=i;}}printf("%d %d",max,index);return 0;}
练习7-3 将数组中的数逆序存放 (20 分)
#includeint main(){int n,i;scanf("%d",&n);int a[n];for(i=n-1;i>=0;i--)scanf("%d",&a[i]);for(i=0;i<n-1;i++)printf("%d ",a[i]);printf("%d",a[n-1]);return 0;}
练习7-4 找出不是两个数组共有的元素 (20 分)
#include int main() {int n, m, i, j, flag,cnt=0;scanf("%d", &n);int a[n];for (i = 0; i < n; i++)scanf("%d", &a[i]);scanf("%d", &m);int b[m];int s[m+n];for (j = 0; j < m; j++)scanf("%d", &b[j]);for (i = 0; i < n; i++) {flag = 0;for (j = 0; j < m; j++) {if (a[i] == b[j]) {flag = 1;break;}}if (flag == 0) {s[cnt]=a[i];cnt++;}}for (j = 0; j < m; j++) {flag = 0;for (i = 0; i < n; i++) {if (a[i] == b[j]) {flag = 1;break;}}if (flag == 0) {s[cnt]=b[j];cnt++;}}printf("%d",s[0]);for(i=1;i<cnt;i++){flag=0;for(j=0;j<i;j++){if(s[i]==s[j]){flag=1;break;}}if(flag==0)printf(" %d",s[i]);}return 0;}
练习7-7 矩阵运算 (20 分)
#includeint main(){int i,n,j,s=0;scanf("%d",&n);int a[n][n];for(i=0;i<n;i++)for(j=0;j<n;j++)scanf("%d",&a[i][j]);for(i=0;i<n;i++)for(j=0;j<n;j++)if(j!=n-1-i&&i!=n-1&&j!=n-1)s+=a[i][j];printf("%d",s);return 0;}
练习7-8 方阵循环右移 (20 分)
#includeint main(){int m,n,i,j;scanf("%d %d",&m,&n);int a[m][n],b[m][n];m=m%n;for(i=0;i<=n-1;i++)for(j=0;j<=n-1;j++)scanf("%d ",&a[i][j]);for(i=0;i<=n-1;i++){for(j=0;j<=n-1;j++){if(j+m>(n-1))b[i][j+m-n]=a[i][j];else b[i][j+m]=a[i][j];}}for(i=0;i<=n-1;i++){for(j=0;j<=n-1;j++)printf("%d ",b[i][j]);printf("\n");}return 0;}
练习7-9 计算天数 (15 分)
#include int main(){int i,year,month,day,n=0,a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};scanf("%d/%d/%d",&year,&month,&day);if((year%4==0&&year%100)||year%400==0)a[2]=29;for(i=1;i<month;i++)n+=a[i];printf("%d",n+day);return 0;}
练习7-10 查找指定字符 (15 分)
#includeint main(){char ch,c[80];int i=0,j,flag=0,index;ch=getchar();getchar();while((c[i]=getchar())!='\n')i++;for(j=0;j<i;j++)if(ch==c[j]){index=j;flag=1;}if(flag==1)printf("index = %d",index);else printf("Not Found");return 0;}
练习7-11 字符串逆序 (15 分)
#include int main(){int n=0,i;char a[80];while((a[n]=getchar())!='\n')n++;for(i=n-1;i>=0;i--)printf("%c",a[i]);return 0;}
习题7-1 选择法排序 (20 分)
#includeint main(){int i,j,n,temp;scanf("%d",&n);int a[n];for(i=0;i<n;i++)scanf("%d",&a[i]);for(i=0;i<n;i++){temp = a[i];for(j=i;j<n;j++)if(a[j]>a[i]){a[i] = a[j];a[j] = temp;temp = a[i];}}printf("%d",a[0]);for(int i=1;i<n;i++)printf(" %d",a[i]);}
习题7-2 求一批整数中出现最多的个位数字 (20 分)
#includeint main(){int n,cnt_max,i;scanf("%d",&n);int a[n],b[10]={0};for(i=0;i<n;i++)scanf("%d",&a[i]);for(i=0;i<n;i++){do{b[a[i]%10]++;a[i]/=10;}while(a[i]!=0);}cnt_max=b[0];for(i=1;i<10;i++)if(cnt_max<b[i])cnt_max=b[i];printf("%d:",cnt_max);for(i=0;i<10;i++)if(b[i]==cnt_max)printf(" %d",i);return 0;}
习题7-3 判断上三角矩阵 (15 分)
#include int main(){int T,i,j,k,l,flag;scanf("%d",&T);int a[T];int b[T][11][11];for(i=0;i<T;i++){scanf("%d",&a[i]);j = a[i];for(k=0;k<j;k++)for(l=0;l<j;l++)scanf("%d",&b[i][k][l]);}for(i=0;i<T;i++){j = a[i];flag = 1;for(k=1;k<j;k++){for(l=0;l<k;l++){if(b[i][k][l]){flag=0;break;}}if(!flag)break;}if(flag)printf("YES\n"); else{printf("NO\n");flag = 0;}}return 0;}
习题7-4 求矩阵各行元素之和 (15 分)
#includeint main(){int m,n,sum=0;scanf("%d %d",&m,&n);int a[m][n],i,j;for(i=0;i<m;i++)for(j=0;j<n;j++)scanf("%d",&a[i][j]);for(i=0;i<m;i++){for(j=0;j<n;j++)sum+=a[i][j];printf("%d\n",sum);sum=0;}return 0;}
习题7-5 找鞍点 (20 分)
#includeint main(){int n,i,j,k;int max,flag;scanf("%d",&n);int a[n][n];for(i=0;i<n;i++)for(j=0;j<n;j++)scanf("%d",&a[i][j]);for(i=0;i<n;i++){max=0;for(j=0;j<n;j++)if(a[i][j]>=a[i][max])max=j;flag=1;for(k=0;k<n;k++)if(a[i][max]>a[k][max])flag=0;if(flag){printf("%d %d\n",i,max);break;}}if(flag==0)printf("NONE");return 0;}
习题7-6 统计大写辅音字母 (15 分)
参考答案1
if分支
#includeint check ( char c ){if( c<'A' || c>'Z' ) return 0;if(c!='A'&&c!='E'&&c!='I'&&c!='O'&&c!='U')return 1;else return 0;}int main(){char a[80];int cnt=0,i=0;while((a[i]=getchar())!='\n')i++;for(i=0;a[i]!='\n';i++)cnt+=check (a[i]);printf("%d",cnt);return 0;}
参考答案2
switch-case分支
#include int check ( char c ){int ret;if ( c<'A' || c>'Z' ) return 0;switch (c) {case 'A': case 'E': case 'I': case 'O': case 'U': ret=0; break;default: ret=1; break;}return ret;}int main(){char c;int cnt = 0;while ( (c=getchar())!='\n' ) cnt += check(c);printf("%d\n", cnt);return 0;}
习题7-7 字符串替换 (15 分)
参考答案1
#include
#include #include #include int main(){char str[81];gets(str);for( int i=0; i<strlen(str); i++ )if( isupper(str[i]) )str[i]='A'+'Z'-str[i];for( int i=0; i<strlen(str); i++ )printf("%c",str[i]);return 0;}
参考答案2
自定义函数
#include #include int isupper(char c){int isupper=0;if(c>='A'&&c<='Z')isupper=1;return isupper;}int main(){char str[81];gets(str);for( int i=0; i<strlen(str); i++ )if( isupper(str[i]) )str[i]='A'+'Z'-str[i];for( int i=0; i<strlen(str); i++ )printf("%c",str[i]);return 0;}
习题7-8 字符串转换成十进制整数 (15 分)
#includeint main(){int i=0,j,flag=0,n=0;char ch,a[80];a[0]='\0';while((ch=getchar())!='#'){if(ch=='-'&&a[0]=='\0'){a[i]=ch;i++;}if((ch>='a'&&ch<='f')||(ch>='A'&&ch<='F')||(ch>='0'&&ch<='9')){a[i]=ch;i++;flag=1;}}if(a[0]=='-'&&flag)printf("-");j=i;for(i=0;i<j;i++){if(a[i]>='0'&&a[i]<='9')n=n*16+a[i]-'0';else if(a[i]>='A'&&a[i]<='Z')n=n*16+a[i]-'A'+10;else if(a[i]>='a'&&a[i]<='z')n=n*16+a[i]-'a'+10;}printf("%d",n);return 0;}
习题8-7 字符串排序 (20 分)
#include#include#define N 5#define M 80int main(){char str[N][M]={'0'};int i,j;for(i=0;i<N;i++)scanf("%s",str[i]);char temp[M];for(i=0;i<N;i++){for(j=i+1;j<N;j++){if(strcmp(str[i],str[j]) >= 0){strcpy(temp, str[i]);strcpy(str[i], str[j]);strcpy(str[j], temp);}}}printf("After sorted:\n");for(int i=0;i<N;i++)printf("%s\n",str[i]);return 0;}
习题8-10 输出学生成绩 (20 分)
本题题干建议使用动态内存分配
#include#includeint main(){int n,i;scanf("%d",&n);float *score,sum,average,min,max;score=(float*)malloc(sizeof(n*score[0]));scanf("%f",&score[0]);min=score[0];max=score[0];sum=score[0];for(i=1;i<n;i++){scanf("%f",&score[i]);if(score[i]>max)max=score[i];if(score[i]<min)min=score[i];sum+=score[i];}average=sum/n;printf("average = %.2f\n",average);printf("max = %.2f\n",max);printf("min = %.2f",min);free(score);return 0;}
习题9-1 时间换算 (15 分)
#include struct time {int h, m, s;} T;int main(){int n;scanf("%d:%d:%d\n", &T.h, &T.m, &T.s);scanf("%d", &n);T.s += n;if (T.s >= 60) {T.s -= 60;T.m++;if (T.m >=60) {T.m -= 60;T.h++;if (T.h==24) T.h = 0;}}printf("%02d:%02d:%02d\n", T.h, T.m, T.s);return 0;}
习题9-3 平面向量加法 (15 分)
#include struct Vec {double x, y;};int main(){struct Vec v1, v2;double x,y;scanf("%lf %lf %lf %lf", &v1.x, &v1.y, &v2.x, &v2.y);x = v1.x + v2.x;y = v1.y + v2.y;if ((x>-0.05) && (x<0)) x = 0.0;if ((y>-0.05) && (y<0)) y = 0.0;printf("(%.1f, %.1f)", x, y);return 0;}
习题9-4 查找书籍 (20 分)
参考答案1
#include #include struct Book{double price;char name[31];};int main(){int n;scanf("%d",&n);struct Book book[n];int i,max = 0,min = 0;for(i=0; i<n; i++){getchar();gets(book[i].name);scanf("%lf",&book[i].price);if(book[i].price>book[max].price)max = i;if(book[i].price<book[min].price)min = i;}printf("%.2lf, %s\n",book[max].price,book[max].name);printf("%.2lf, %s\n",book[min].price,book[min].name);return 0;}
参考答案2
学习使用enum
#include #include #include typedef enum{false, true} bool;struct book_info {double price;char name[31];};int main(){int n;struct book_info highest, lowest, cur;bool first = true;scanf("%d\n", &n);while(n--) {gets(cur.name);scanf("%lf\n", &cur.price);if(first) {highest.price = cur.price;strcpy(highest.name, cur.name);lowest.price = cur.price;strcpy(lowest.name, cur.name);first = false;continue;}if(cur.price > highest.price) {highest.price = cur.price;strcpy(highest.name, cur.name);} else if(cur.price < lowest.price) {lowest.price = cur.price;strcpy(lowest.name, cur.name);}}printf("%.2f, %s\n", highest.price, highest.name);printf("%.2f, %s\n", lowest.price, lowest.name);return 0;}
习题9-5 通讯录排序 (20 分)
参考答案1
自定义选择排序
#include #include struct friends {char name[11];int birth;char tele[18];} friend[11];void sort(struct friends friend[],int n){struct friends temp;for(int i=0;i<n;i++){for(int j=i+1;j<n;j++){if(friend[i].birth>friend[j].birth){temp=friend[i];friend[i]=friend[j];friend[j]=temp;}}}}int main() {int n, i;scanf("%d", &n);for (i = 0; i < n; i++) {getchar();scanf("%s%d%s", friend[i].name, &friend[i].birth, friend[i].tele);}sort(friend, n);for (i = 0; i < n; i++) printf("%s %d %s\n", friend[i].name, friend[i].birth, friend[i].tele);return 0;}
参考答案2
qsort排序
#include #include struct friends {char name[11];int birth;char tele[18];} friend[9];int cmp(const void *a, const void *b) {return ((struct friends *)a)->birth > ((struct friends *)b)->birth;}int main() {int n, i;scanf("%d", &n);for (i = 0; i < n; i++) {getchar();scanf("%s%d%s", friend[i].name, &friend[i].birth, friend[i].tele);}qsort(friend, n, sizeof(struct friends), cmp);for (i = 0; i < n; i++) printf("%s %d %s\n", friend[i].name, friend[i].birth, friend[i].tele);return 0;}