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

下载本文档

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

文档简介

1、6.1下面代码的功能是將换为5八件功能是:如果用户输入的是非法字符或者不在合理区间内的数据(例如输入的是a,或者102,或45等),则程序输出Input error!,并允许用户重新输入,直到输入合法数据为止,并将其转换为5分制输出。目前程序存 在错误,请将其修改正确。并按照下面给出的运行示例检查程序。#include<stdio. h>#include <string h>int mainOchar score100;int flag = 0, i, s;char grade;printf (''Please input score:n);while

2、(1)flag=0;scanfscore);for (i = 0; i < strlen(score); i+)if (score li >= ' O' && scorei <= '9')continue;elseflag 二 1;break;s = atoi(score);if (s < 0 s > 100 flag 二二 1)printf("Input error!n);printf("Please input score:n);continue;elsebreak;s = atoi (sco

3、re);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分)题目内容:当n=4, a=2,表示计算编程计算a+aa+aaa+aaa (n个a)的值,n和a的值由键盘输入。例如 2+22+222+2222 的值。#includ

4、e<stdio. h>#includeGnath h>int mainOint n, a, i, j;double p二0, q二0;printf (z/Input a, n: n/z);scanf ( /?%d, %dz &a, &n);for (i=l; i<=n; i+)for(j=0, p=0;j<i;j+)p=p+a*pow(10, j);q二p+q;printf (z/sum=% Ofn", q);return 0;6.3搬砖问题(4分)题目内容:n块砖(27<n<=77 ), 36人搬,男搬4,女搬3.两个小孩抬

5、一块砖,要求一次搬完问男人、女人和小 孩各需女少人?请用穷举法編程求解"的危耍求从键盘输入。输出结果按照男人数址升序给出(见下面示 例3o#include stdio. hmainOint a, b, c;long n, i, t, s = 0;printf C'Input n(27<n<=77): n/z);scanf&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 +

6、3*b + c/ 2=n&& c%2 = 0 && a+b+c=36)printf ("men=%d, women=%d, children=%dn/z, a, b, c);6.4编程输出某年某月有多少天(考虑到闰年)。(5分)题目内容:从键盘输入一个年份和丿I份.输出该丿J有多少天(考定闰年)> 用switch语句编程。#include <stdio. h>int mainOint year, month, day;printf ("Input year, month:n");scanf C'%d,&am

7、p;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=-l;printf (,?Input error !rT);if (year%4=0&&year%100!二 0 jear%4000) &&a

温馨提示

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

评论

0/150

提交评论