




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、4.1 编写一个程序,输入A,B,C三个学生的考试分数,输出分数居中的那个学生名(A、B或C)及考试分数。#include 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 score of C:n); scanf(%f, &c); if (a-b)*(a-c) = 0) printf(A gets the score %.1f,
2、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从终端输入。应考虑实根、复数根和无解的情况。#include#includeint 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 -
3、4*a*c;if(d 0)x1 = (sqrt(d) - b) / (2*a);x2 = -(sqrt(d) + b) / (2*a);printf(Two real 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
4、= -c/b;printf(One real result:n%.6fn, x1);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, &m); printf(Enter day:); scanf(%d, &d
5、); if(m12) 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 5: days += 30; case 4: days += 31; case 3: days += (y%4=0 & y%100!=0 | y%400=0 ? 29 : 28); ca
6、se 2: days += 31; printf(%dn, days); return 0;函数方法:#include 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 | 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); prin
7、tf(Enter day:); scanf(%d, &d); if(m12 | dmdays(y, m) printf(Input error!n); else days = d; while(m1) 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%的税金;4000x5000,收取25%的税金。输入工资金额,输出应收取税金额度,要求分别用if语句和switc
8、h语句来实现。if方法:#include int main()float x = 0; printf(input the salary:); scanf(%f,&x); if(x0) printf(Input error!n); else if(x1000) printf(0.00n); else if(x2000) printf(%.2fn,x*0.05); else if(x3000) printf(%.2fn,x*0.1); else if(x4000) printf(%.2fn,x*0.15); else if(x5000) printf(%.2fn,x*0.2); else prin
9、tf(%.2fn,x*0.25); return 0;switch方法:#include int main() float x ; int xCase = 0; printf(input the salary:); scanf(%f,&x); if(x0) 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; c
10、ase 3: printf(%.2fn,x*0.15); break; case 4: printf(%.2fn,x*0.2); break; default: printf(%.2fn,x*0.25); return 0;4.5 输入两个实数和一个四则运算符(+、-、*、/),根据运算符执行相应的运算并输出运算结果,分别用if语句和switch语句来实现。if方法:#includeint main(void)float a, b, result;char c;printf(Please key in 2 numbers:);scanf(%f%f, &a, &b);getchar();prin
11、tf(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 = *)result = 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);elseprint
12、f(Error!No result.n);elseprintf(Error!No result.n);return 0;switch方法:#includeint main(void)float a, b, result;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;cas
13、e -:result = a-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 result.n);return 0;4.6 统计输入正文中空格字符、制表符和换行符的个数。#includeint mai
14、n(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);return 0;4.7 编写一个程序,将输入的一行字符复制到输出,复制过程中将一个以上的空格字符用一个空格代替。#include int main() char c; int flag = 0; do c = getchar(); if(c!=
15、 ) putchar(c); flag = 0; else if(!flag) putchar(c); flag = 1; while(c != n); return 0;4.8 将输入的正文复制到输出,复制过程中删去每个输入行的前导空格。#includeint main(void)int ch, flag, i=0;char s800;while(ch=getchar() != EOF & i =20,每行输出8个数。计算公式为:a1=1,a2=1,an=an-1+an-2(n3)。#includeint main(void)int n, i, fib100;printf(Please ke
16、y in a number:);scanf(%d, &n);fib0 = 1;printf(%6d, fib0);fib1 = 1;printf(%6d, fib1);for(i=2; in; i+)fibi = fibi-1 + fibi-2;printf(%6d, fibi);if(i+1)%8 = 0)putchar(n);return 0;4.11 编写一个程序,输入两个整数,求它们的最大公约数和最小公倍数。#includeint main() int a,b,num1,num2,temp; printf(Input a & b:); scanf(%d%d,&num1,&num2);
17、a=num1; b=num2; if(ab) temp=a; a=b; b=temp; 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,为互质数*/#includeint main(void)int m, n, i, min;print
18、f(Please key in two number:);scanf(%d%d, &m, &n);min = mn ? n : m;for(i=2; imin)printf(Yesn);elseprintf(Falsen);return 0;4.13 编写一个程序,验证哥德巴赫猜想:任何充分大(=4)的偶数都可以用两个素数之和表示,将4100中所有偶数分别用两个素数之和的形式输出。(例如:4=2+2,100=3+97)#include #include 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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 主管护师考试优劣势分析与试题及答案
- 学员分享的执业药师试题及答案
- 与文化相伴的行政管理试题及答案
- 行政管理与文化活动试题及答案
- 行政决策中的多方利益协调的试题及答案
- 临床护理评估工具试题及答案
- 自考行政管理试题实战及答案解读
- 大学语文考试综合能力测评试题及答案
- 2025年护士资格考试准备小贴士试题及答案
- 执业护士中的自我管理试题及答案
- YY/T 1544-2017环氧乙烷灭菌安全性和有效性的基础保障要求
- GB/T 19582.3-2008基于Modbus协议的工业自动化网络规范第3部分:Modbus协议在TCP/IP上的实现指南
- 连续性肾替代治疗(CRRT)详细介绍课件
- 建筑工程质量检测课件
- 计算机网络的毕业设计(5篇)
- 2022年中国建筑集团有限公司招聘笔试题库及答案解析
- 温泉度假设施造价预算
- 青少年创意编程试题
- RULES OF ORIGIN 原产地规则
- 国内旅游出团通知书(新版)
- LETTEROFINTENTION意向书范本
评论
0/150
提交评论