中国大学MOOC 哈工大 C语言程序设计精髓第六周编程题答案_第1页
中国大学MOOC 哈工大 C语言程序设计精髓第六周编程题答案_第2页
中国大学MOOC 哈工大 C语言程序设计精髓第六周编程题答案_第3页
中国大学MOOC 哈工大 C语言程序设计精髓第六周编程题答案_第4页
全文预览已结束

下载本文档

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

文档简介

6.1下面代码的功能是将百分制成绩转换为5分制成绩,具体功能是:如果用户输入的是非法字符或者不在合理区间内的数据(例如输入的是a,或者102,或-45等),则程序输出Input error!,并允许用户重新输入,直到输入合法数据为止,并将其转换为5分制输出。目前程序存在错误,请将其修改正确。并按照下面给出的运行示例检查程序。#include#include int main()char score100;int flag = 0, i, s;char grade;printf(Please input score:n);while (1)flag=0;scanf(%s, score);for (i = 0; i = 0 & scorei = 9)continue;elseflag = 1;break;s = atoi(score);if (s 100 | flag = 1)printf(Input error!n);printf(Please input score:n);continue;elsebreak;s = atoi(score);if (s = 90)grade = A;else if (s = 80)grade = B;else if (s = 70)grade = C;else if (s = 60)grade = D;elsegrade = E;printf(grade: %cn, grade);return 0;6.2编程计算a+aa+aaa+aaa(n个a)的值(4分)题目内容:编程计算 a+aa+aaa+aaa(n个a)的值,n和a的值由键盘输入。例如,当n=4,a=2,表示计算2+22+222+2222的值。#include#includeint main()int n,a,i,j;double p=0,q=0;printf(Input a,n:n);scanf( %d,%d,&a,&n);for(i=1;i=n;i+)for(j=0,p=0;ji;j+)p=p+a*pow(10,j);q=p+q;printf(sum=%.0fn,q);return 0;6.3搬砖问题(4分)题目内容:n块砖(27n=77),36人搬,男搬4,女搬3,两个小孩抬一块砖,要求一次搬完,问男人、女人和小孩各需多少人?请用穷举法编程求解,n的值要求从键盘输入。输出结果按照男人数量升序给出(见下面示例3)。#include stdio.hmain()int a, b, c;long n, i, t, s = 0;printf(Input n(27n=77):n);scanf(%d, &n);for (a = 0; 4 * a = n; a+)for (b = 0; 4 * a + 3 * b = n; b+)for (c = 0; 4 * a + 3 * b + c / 2 = n; c += 2)if (4 * a + 3 * b + c / 2 = n & c%2 = 0 & a+b+c=36)printf(men=%d,women=%d,children=%dn, a, b, c);6.4编程输出某年某月有多少天(考虑到闰年)。(5分)题目内容:从键盘输入一个年份和月份,输出该月有多少天(考虑闰年),用switch语句编程。#include int main()int year,month,day;printf(Input year,month:n);scanf(%d,%d,&year,&month);switch(month)case 1: day=31;break;case 2: day=28;break;case 3: day=31;break;case 4: day=30;break;case 5: day=31;break;case 6: day=30;break;case 7: day=31;break;case 8: day=31;break;case 9: day=30;break;case 10: day=31;break;case 11: day=30;break;case 12: day=31;break;default:day=-1;printf(Input error!n);

温馨提示

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

评论

0/150

提交评论