第一范文网 - 专业文章范例文档资料分享平台

哈工大C语言程序设计精髓MOOC慕课 周编程题答案

来源:用户分享 时间:2025/8/17 15:27:01 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

\下面代码的功能是将百分制成绩转换为5分制成绩,具体功能是:如果用户输

入的是非法字符或者不在合理区间内的数据(例如输入的是a,或者102,或-45等),则程序输出 Input error!,并允许用户重新输入,直到输入合法数据为止,并将其转换为5分制输出。目前程序存在错误,请将其修改正确。并按照下面给出的运行示例检查程序

*/

#include<> #include <> int main() {

char score[100]; int flag = 0, i, s; char grade;

printf(\ while (1) {

flag=0;

scanf(\

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(\ printf(\ 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(\ return 0; }\

\

#include<> int main() {

int n,a,i,j; double p=0,q=0; printf(\ scanf( \ for(i=1;i<=n;i++) {

for(j=0,p=0;j

p=p+a*pow(10,j); } q=p+q; }

printf(\ return 0; }\

\

n块砖( 27

men=0,women=4,children=32 程序的运行结果示例2: Input n(27

men=3,women=3,children=30 程序的运行结果示例3: Input n(27

men=2,women=14,children=20 men=7,women=7,children=22 men=12,women=0,children=24 输入提示: \输入格式: \

输出格式:\*/

#include \

main() {

printf(\ long n, i, t, s = 0; scanf(\ int a, b, c;

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(\ } }\

\

int main()

{int year,month,day;

printf(\scanf(\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(\}

if((year%4==0&&year0!=0||year@0==0)&&month==2) if (day!=-1)

printf(\return 0; }\

\

unsigned int ComputeAge(unsigned int n){

day=29; } main() {

int i, j, k, s = 23, n, c, age; scanf(\

printf(\}\

\

int gys(int a,int b) {

int r; r=a%b;

if(r==0) return b; else return gys(b,r); } main() {

printf(\ int a,b;

scanf(\ if (a<=0 || b<=0){

printf(\

} else

printf(\}\

\

int median(int a, int b, int c) {

if(a

if(b

else{return a

No repeated digit! 输入提示:\输入格式: \输出格式:

有重复数字,输出信息: \

没有重复数字,输出信息: \*/

#include <> int main() {

int log[10]= {0},a[100]; int b,i=0,n,c,d; printf(\ scanf(\ while(n!=0) {

b=n; n/=10; a[i]=b; i++; } a[i]=n; int flag=0; for(c=0; c

if(a[c]==a[d])

{

flag=1; break; } }

str[i] != str[i+1],则计数器重新初始化为1。遍历结束时,函数返回程序运行结果示例1: Input a string: 5:5

程序运行结果示例2: Input a string: sgf222257↙ 2:4

输入提示信息:\输入格式: 用gets()输入字符串 输出格式:\*/

#include <> #include <> int main() {

char a[80];

max的值。 int b, i,j,t=1,tl,num=0; printf(\ gets(a);

for (i=0;i

for (j=i+1;j

if (t>tl){ tl=t; num=i; } } }

printf(\}\

\

从键盘输入一串字符(假设字符数少于8个),以回车表示输入结束,编程将其中的数字部分转换为整型数并以整型的形式输出。 函数原型为 int Myatoi(char str[]);

其中,形参数组str[]对应用户输入的字符串,函数返回值为转换后的整型数。 解题思路的关键是:1)判断字符串中的字符是否是数字字符;2)如何将数字字符转换为其对应的数字值;3)如何将每一个转换后的数字值加起来形成一个整型数。 程序运行结果示例1: Input a string:7hg09y↙ 709

程序运行结果示例2: Input a string:9w2k7m0↙ 9270

程序运行结果示例3: Input a string:happy↙ 0

输入提示信息:\输入格式: \输出格式:\*/

#include <> #include <>

#include <> int Myatoi(char str[]){ int i,j;

for (i=0,j=0;str[i]!='\\0';i++){

if (str[i] >='0' && str[i]<='9'){ str[j]=str[i]; j++; } } str[j]='\\0'; return atoi(str); } int main() {

char s[7];

printf(\ scanf(\ printf(\ printf(\ return 0; }\

\输入n个整数(n从键盘输入,假设n的值不超过100),按奇偶数分成两组并

输出。输出两行,第一行为所有奇数,第二行为所有偶数,保持数据的相对顺序与输入顺序相同。 函数原型如下所示:

void Seperate(int a[], int n);?ame);

printf(\

printf(\ printf(\not?(Y/N):\ printf(\not?(Y/N):\

printf(\papers:\ stu[i].scholarship=0;

if (stu[i].finalScore>80 && stu[i].paper >=1) stu[i].scholarship+=8000;

if (stu[i].finalScore>85 && stu[i].classScore> 80) stu[i].scholarship+=4000;

if (stu[i].finalScore>90) stu[i].scholarship+=2000;

if (stu[i].finalScore>85 && stu[i].west=='Y') stu[i].scholarship+=1000; if (stu[i].classScore> 80 && stu[i].work=='Y') stu[i].scholarship+=850; printf(\

}

int ts=stu[0].scholarship,k; for (i=1;i

if (ts

printf(\ /*

1) 院士奖学金:期末平均成绩高于80分(>80),并且在本学期内发表1篇或1篇以上论文的学生每人均可获得8000元;

2) 五四奖学金:期末平均成绩高于85分(>85),并且班级评议成绩高于80分(>80)的学生每人均可获得4000元;

3) 成绩优秀奖:期末平均成绩高于90分(>90)的学生每人均可获得2000元; 4) 西部奖学金:期末平均成绩高于85分(>85)的西部省份学生每人均可获得1000元;

5) 班级贡献奖:班级评议成绩高于80分(>80)的学生干部每人均可获得850元; */ return 0; }\

\

请编写一个简单的23 根火柴游戏程序,实现人跟计算机玩这个游戏的程序。为了方便程序自动评测,假设计算机移动的火柴数不是随机的,而是将剩余的火柴根数对3求余后再加1来作为计算机每次取走的火柴数。如果计算机打算移走的火柴数等于剩下的火柴数,则将计算机打算移走的火柴数减1。但是计算机不可以不取,剩下的火柴数为1时,必须取走1根火柴。假设游戏规则如下: 1)游戏者开始拥有23根火柴棒;

2)每个游戏者轮流移走1 根、2 根或3 根火柴; 3)谁取走最后一根火柴为失败者。 程序运行结果示例1: Game start!

Note: the maximum number is 3

Please enter the number of matches you are moving: 5↙

The number you entered is wrong,please re-enter! Please enter the number of matches you are moving: 3↙

The number of matches you are moving is:3 The number of matches left is:20

The number of matches that have been moved by the computer is:3 The number of matches left is:17

Please enter the number of matches you are moving: 1↙

The number of matches you are moving is:1 The number of matches left is:16

The number of matches that have been moved by the computer is:2 The number of matches left is:14

Please enter the number of matches you are moving: 2↙

The number of matches you are moving is:2 The number of matches left is:12

The number of matches that have been moved by the computer is:1 The number of matches left is:11

Please enter the number of matches you are moving: 3↙

The number of matches you are moving is:3 The number of matches left is:8

The number of matches that have been moved by the computer is:3 The number of matches left is:5

Please enter the number of matches you are moving: 1↙

The number of matches you are moving is:1 The number of matches left is:4

The number of matches that have been moved by the computer is:2 The number of matches left is:2

Please enter the number of matches you are moving: 1↙

The number of matches you are moving is:1 The number of matches left is:1

The number of matches that have been moved by the computer is:1 The number of matches left is:0 Congratulations!You won! 程序运行结果示例2: 3 3 2 1 3 1

游戏开始提示信息:\

\

提示游戏者输入移动的火柴数:\moving:\\n\

游戏者输入错误数据的提示信息:\,please re-enter!\\n\输入格式: \输出格式:

输出被游戏者移动的火柴数:\ 输出被计算机移动的火柴数:\the computer is:%d\\n\

输出被游戏者或计算机移动后剩余的火柴数:\is:%d\\n\

游戏者获胜的输出提示信息:\!You won!\\n\游戏者失败的输出提示信息:\*/

#include <> main() {

printf(\ int i, j, k, s = 23, n, c; while (1) {

printf(\ scanf(\

if (n<=3 && n>=1 && n<=s){ ; } else{

printf(\,please re-enter!\\n\

continue; } s -= n;

printf(\of matches left is:%d\\n\ if (s > 0) {

if (s==3) {

c = 2; }

else if (s==2){ c=1; }

else if (s==1){ c=1; } else {

c = s%3+1;

} s -= c;

printf(\the computer is:%d\\nThe number of matches left is:%d\\n\ if (s==0){

printf(\!You won!\\n\ break; } }

else if (s==0){

printf(\ } } }\

\

题目内容:请输入星期几的第一个字母(不区分大小写)来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母(小写),否则输出“data error”。 程序运行结果示例1:

please input the first letter of someday: S↙

please input second letter: u↙

sunday

程序运行结果示例2:

please input the first letter of someday: F↙ friday

程序运行结果示例2:

please input the first letter of someday: h↙ data error

第一个字母的输入提示信息:\第二个字母的输入提示信息:\用户输入错误提示信息:\输入格式: \(注意:%c前面有一个空格) 输出格式:

星期一:\星期二:\星期三:\星期四:\星期五:\星期六:\星期日:\*/

#include<> main() { char i,j;

printf(\ scanf(\ switch(i) {

case 'm':case 'M':

printf(\ break; case 'w':case 'W':

printf(\ break; case 'f':case 'F':

printf(\ break; case 't':case 'T':

printf(\ scanf(\ j=getchar(); if (j=='u')

printf(\ else if (j=='h')

printf(\ else printf(\ break; case 's':case 'S':

printf(\ scanf(\ j=getchar(); if (j=='a')

printf(\ else if (j=='u')

printf(\ else printf(\ break;

default : printf(\ } }\

哈工大C语言程序设计精髓MOOC慕课 周编程题答案.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c7dp1i3apv376vac3ljxx41z4g1sgcd0187b_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top