中国大学MOOC-哈工大-C语言程序设计精髓第六-十二周编程题答案.docVIP

  • 59
  • 0
  • 约3.17万字
  • 约 67页
  • 2020-11-07 发布于江苏
  • 举报

中国大学MOOC-哈工大-C语言程序设计精髓第六-十二周编程题答案.doc

6.1 下面代码功效是将百分制成绩转换为5分制成绩,具体功效是:假如用户输入是非法字符或不在合理区间内数据(比如输入是a,或102,或-45等),则程序输出?Input error!,并许可用户重新输入,直到输入正当数据为止,并将其转换为5分制输出。现在程序存在错误,请将其修更正确。并根据下面给出运行示例检验程序。 #includestdio.h #include string.h int main() { char score[100]; int flag = 0, i, s; char grade; printf(Please input score:\n); while (1) { flag=0; scanf(%s, score); for (i = 0; i strlen(score); i++) { if (score[i] = 0 score[i] = 9) { continue; } else { flag = 1; break; } } s = atoi(score); if (s 0 || s 100 || flag == 1) { printf(Input error!\n); printf(Please input score:\n); continue; } else{ break; } } 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; } else { grade = E; } printf(grade: %c\n, grade); return 0; } 6.2 编程计算a+aa+aaa+…+aa…a(n个a)值(4分) 题目内容: 编程计算 a+aa+aaa+…+aa…a(n个a)值,n和a值由键盘输入。比如,当n=4,a=2,表示计算2+22+222+2222值。 #includestdio.h #includemath.h int 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=%.0f\n,q); return 0; } 6.3 搬砖问题(4分) 题目内容: n块砖(?27n=77?),36人搬,男搬4,女搬3,两个小孩抬一块砖,要求一次搬完,问男人、女人和小孩各需多少人?请用穷举法编程求解,n值要求从键盘输入。输出结果根据男人数量升序给出(见下面示例3)。 #include stdio.h main() { 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=%d\n, a, b, c); } } 6.4 编程输出某年某月有多少天(考虑到闰年)。(5分) 题目内容: 从键盘输入一个年份和月份,输出该月有多少天(考虑闰年),用switch语句编程。 #include stdio.h 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: d

文档评论(0)

1亿VIP精品文档

相关文档