第4章 习题解答_第1页
第4章 习题解答_第2页
第4章 习题解答_第3页
第4章 习题解答_第4页
第4章 习题解答_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、4.1 编写一个程序,输入A,B,C三个学生的考试分数,输出分数居中的那个学生名(A、B或C)及考试分数。#include <stdio.h>int main() float a, b, c; printf("Please enter the score of A:n"); scanf("%f", &a); printf("Please enter the score of B:n"); scanf("%f", &b); printf("Please enter the sco

2、re of C:n"); scanf("%f", &c); if (a-b)*(a-c) <= 0) printf("A gets the score %.1f", a); else if (b-a)*(b-c) <= 0) printf("B gets the score %.1f", b); else printf("C gets the score %.1f", c); return 0;4.2 编程求一元二次方程ax2+bx+c=0的根,系数a,b,c从终端输入。应考虑实根、复

3、数根和无解的情况。#include<stdio.h>#include<math.h>int main(void)float a, b, c, d, x1, x2;printf("Please key in 3 numbers:");scanf("%f%f%f", &a, &b, &c);if(a != 0)d = b*b - 4*a*c;if(d > 0)x1 = (sqrt(d) - b) / (2*a);x2 = -(sqrt(d) + b) / (2*a);printf("Two re

4、al results:n%.6fn%.6fn", x1, x2);else if(d = 0)x1 = -b/(2*a);printf("One real result:n%.6fn", x1);elsex1 = -b/(2*a);x2 = sqrt(-d)/(2*a);printf("Two virsual results:n%.3f+%.3fin%.3f-%.3fin", x1, x2, x1, x2);else if(b != 0)x1 = -c/b;printf("One real result:n%.6fn", x

5、1);else if(c!=0)printf("Error!No result.n");elseprintf("Anyone is result.n");return 0;4.3 输入一个日期(年、月、日),计算并输出该日期是这一年的第几天。switch方法:int main() int y, m, d, days; printf("Enter year:"); scanf("%d", &y); printf("Enter month:"); scanf("%d",

6、&m); printf("Enter day:"); scanf("%d", &d); if(m<1 | m>12) printf("Input error!n"); else days = d; switch(m) case 12: days += 30; case 11: days += 31; case 10: days += 30; case 9: days += 31; case 8: days += 31; case 7: days += 30; case 6: days += 31; case

7、 5: days += 30; case 4: days += 31; case 3: days += (y%4=0 && y%100!=0 | y%400=0 ? 29 : 28); case 2: days += 31; printf("%dn", days); return 0;函数方法:#include <stdio.h>int mdays(int y, int m)if (m=2) return y%4=0 && y%100!=0 | y%400=0 ? 29 : 28;else if (m=4 | m=6 | m=9

8、| m=11) return 30;else return 31;int main()int y, m, d, days; printf("Enter year:"); scanf("%d", &y); printf("Enter month:"); scanf("%d", &m); printf("Enter day:"); scanf("%d", &d); if(m<1 | m>12 | d>mdays(y, m) printf(

9、"Input error!n"); else days = d; while(m>1) days += mdays(y, m-1); m-; printf("%dn", days); return 0;4.4 假设工资税金按以下方法计算:x < 1000元,不收取税金;1000x <2000,收取5%的税金;2000 x < 3000,收取10%的税金;3000 x < 4000,收取15%的税金;4000x<5000,收取20%的税金;x>5000,收取25%的税金。输入工资金额,输出应收取税金额度,要求分别用

10、if语句和switch语句来实现。if方法:#include <stdio.h>int main()float x = 0; printf("input the salary:"); scanf("%f",&x); if(x<0) printf("Input error!n"); else if(x<1000) printf("0.00n"); else if(x<2000) printf("%.2fn",x*0.05); else if(x<3000

11、) printf("%.2fn",x*0.1); else if(x<4000) printf("%.2fn",x*0.15); else if(x<5000) printf("%.2fn",x*0.2); else printf("%.2fn",x*0.25); return 0;switch方法:#include <stdio.h>int main() float x ; int xCase = 0; printf("input the salary:"); scan

12、f("%f",&x); if(x<0) printf("Input error!n"); else xCase = (int)(x/1000.0); switch(xCase) case 0: printf("0.00n"); break; case 1: printf("%.2fn",x*0.05); break; case 2: printf("%.2fn",x*0.1); break; case 3: printf("%.2fn",x*0.15); bre

13、ak; case 4: printf("%.2fn",x*0.2); break; default: printf("%.2fn",x*0.25); return 0;4.5 输入两个实数和一个四则运算符(+、-、*、/),根据运算符执行相应的运算并输出运算结果,分别用if语句和switch语句来实现。if方法:#include<stdio.h>int main(void)float a, b, result;char c;printf("Please key in 2 numbers:");scanf("%f%

14、f", &a, &b);getchar();printf("Please key in 1 operator:");scanf("%c", &c);if(c = '+')result = a+b;printf("%f+%f=%.6fn", a, b, result);else if(c = '-')result = a-b;printf("%f-%f=%.6fn", a, b, result);else if(c = '*')resu

15、lt = a*b;printf("%f*%f=%.6fn", a, b, result);else if(c = '/')if(b!=0)result = a/b;printf("%f/%f=%.6fn", a, b, result);elseprintf("Error!No result.n");elseprintf("Error!No result.n");return 0;switch方法:#include<stdio.h>int main(void)float a, b, res

16、ult;char c;printf("Please key in 2 numbers:");scanf("%f%f", &a, &b);getchar();printf("Please key in 1 operator:");scanf("%c", &c);switch(c)case '+':result = a+b;printf("%f+%f=%.6fn", a, b, result);break;case '-':result = a

17、-b;printf("%f-%f=%.6fn", a, b, result);break;case '*':result = a*b;printf("%f*%f=%.6fn", a, b, result);break;case '/':if(b!=0)result = a/b;printf("%f/%f=%.6fn", a, b, result);elseprintf("Error!No result.n");break;default:printf("Error!No r

18、esult.n");return 0;4.6 统计输入正文中空格字符、制表符和换行符的个数。#include<stdio.h>int main(void)int a = 0, b = 0, c = 0;int ch;while(ch=getchar() != EOF)if(ch = ' ')a+;else if(ch = 't')b+;else if(ch = 'n')c+;printf("The number of space, tab, enter is :%d, %d, %dn", a, b, c)

19、;return 0;4.7 编写一个程序,将输入的一行字符复制到输出,复制过程中将一个以上的空格字符用一个空格代替。#include <stdio.h>int main() char c; int flag = 0; do c = getchar(); if(c!=' ') putchar(c); flag = 0; else if(!flag) putchar(c); flag = 1; while(c != 'n'); return 0;4.8 将输入的正文复制到输出,复制过程中删去每个输入行的前导空格。#include<stdio.h&g

20、t;int main(void)int ch, flag, i=0;char s800;while(ch=getchar() != EOF && i < 799)si+ = (char)ch;si = 0;flag = 1;printf("nn");for(i=0; si!=0; i+)if(!(flag && si = ' ')putchar(si);if(si = 'n')flag = 1;elseflag = 0;return 0;4.9 输出斐波拉契数列的前n项。例如:11235813n由终端输入

21、,n>=20,每行输出8个数。计算公式为:a1=1,a2=1,an=an-1+an-2(n>3)。#include<stdio.h>int main(void)int n, i, fib100;printf("Please key in a number:");scanf("%d", &n);fib0 = 1;printf("%6d", fib0);fib1 = 1;printf("%6d", fib1);for(i=2; i<n; i+)fibi = fibi-1 + fib

22、i-2;printf("%6d", fibi);if(i+1)%8 = 0)putchar('n');return 0;4.11 编写一个程序,输入两个整数,求它们的最大公约数和最小公倍数。#include<stdio.h>int main() int a,b,num1,num2,temp; printf("Input a & b:"); scanf("%d%d",&num1,&num2); a=num1; b=num2; if(a>b) temp=a; a=b; b=temp

23、; while(b!=0) temp=a%b; a=b; b=temp; printf("The GCD of %d and %d is: %dn",num1,num2,a); printf("The LCM of them is: %dn",num1*num2/a); return 0;4.12 输入两个整数,判断它们是否为互质数并输出判断结果。/*公因数只有1的两个非零自然数,叫做互质数。举例:2和3,公因数只有1,为互质数*/#include<stdio.h>int main(void)int m, n, i, min;printf(&

24、quot;Please key in two number:");scanf("%d%d", &m, &n);min = m>n ? n : m;for(i=2; i<=min; i+)if(!(m%i)if(!(n%i)break;if(i>min)printf("Yesn");elseprintf("Falsen");return 0;4.13 编写一个程序,验证哥德巴赫猜想:任何充分大(>=4)的偶数都可以用两个素数之和表示,将4100中所有偶数分别用两个素数之和的形式输出。(例

25、如:4=2+2,100=3+97)#include <stdio.h>#include <math.h>int Primes(int x); /判断素数函数int main() int i;int num; for(num=4;num<=100;num+=2) for(i=1;i<=num/2;i+) if(Primes(i) && Primes(num-i) printf("%d=%d+%dn", num, i, num-i); return 0; int Primes(int x) int i, j; j = sqrt(x); for( i=2; i<=j; i+) if(x%i=0) break; if( i>j ) return 1; else return 0;4.15 输出10000以内所有这样的完全平方数:a2 = b2*10 + c2。例如,361=192=62*10+12。#include<stdio.h>#define N 10000int main(void)int a, b, c, l, m, n;for(a=1; (l

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论