您好,欢迎来到爱go旅游网。
搜索
您的当前位置:首页哈工大c语言程序设计精髓mooc慕课6-12周编程题答案

哈工大c语言程序设计精髓mooc慕课6-12周编程题答案

来源:爱go旅游网


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

*/

#include<>

#include <>

int main()

{

char score[100];

int flag = 0, i, s;

char grade;

printf(\"\"Please input score:\\n\"\");

while (1)

{

flag=0;

scanf(\"\"%s\"\

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)

{

}

else{

}

}

s = atoi(score);

printf(\"\"Input error!\\n\"\");

printf(\"\"Please input score:\\n\"\");

continue;

break;

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\"\

return 0;

}\"

\"#include<>

#include<>

int main()

{

int n,a,i,j;

double p=0,q=0;

printf(\"\"Input a,n:\\n\"\");

scanf( \"\"%d,%d\"\

for(i=1;i<=n;i++)

{

for(j=0,p=0;j{

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

}

q=p+q;

}

printf(\"\"sum=%.0f\\n\"\

return 0;

}\"

\"/*

n块砖( 27程序的运行结果示例1:

Input n(2728↙

men=0,women=4,children=32

程序的运行结果示例2:

Input n(2736↙

men=3,women=3,children=30

程序的运行结果示例3:

Input n(2760↙

men=2,women=14,children=20

men=7,women=7,children=22

men=12,women=0,children=24

输入提示: \"\"Input n(27输入格式: \"\"%d\"\"

输出格式:\"\"men=%d,women=%d,children=%d\\n\"\"

*/

#include \"\"\"\"

main()

{

printf(\"\"Input n(27long n, i, t, s = 0;

scanf(\"\"%d\"\

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(\"\"men=%d,women=%d,children=%d\\n\"\

}

}\"

\"#include<>

int main()

{int year,month,day;

printf(\"\"Input year,month:\\n\"\");

scanf(\"\"%d,%d\"\

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\"\");

}

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

printf(\"\"%d days\\n\"\

return 0;

day=29;

}\"

\"#include <>

unsigned int ComputeAge(unsigned int n){

}

main()

{

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

scanf(\"\"%d\"\

printf(\"\"The person's age is %u\\n\"\

}\"

\"#include <>

int gys(int a,int b)

{

int r;

r=a%b;

if(r==0) return b;

else return gys(b,r);

}

main()

{

printf(\"\"Input a,b:\"\");

int a,b;

scanf(\"\"%d,%d\"\

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

printf(\"\"Input error!\\n\"\");

}

else

printf(\"\"%d\\n\"\

}\"

\"#include <>

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

{

if(a{

if(belse{return aInput n:

28212

Repeated digit!

程序运行结果示例2:

Input n:

12345↙

No repeated digit!

输入提示:\"\"Input n:\\n\"\"

输入格式: \"\"%ld\"\"

输出格式:

有重复数字,输出信息: \"\"Repeated digit!\\n\"\"

没有重复数字,输出信息: \"\"No repeated digit!\\n\"\"

*/

#include <>

int main()

{

int log[10]= {0},a[100];

int b,i=0,n,c,d;

printf(\"\"Input n:\\n\"\");

scanf(\"\"%d\"\

while(n!=0)

{

b=n%10;

n/=10;

a[i]=b;

i++;

}

a[i]=n;

int flag=0;

for(c=0; cfor(d=c+1; d{

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

{

flag=1;

break;

}

}

str[i] != str[i+1],则计数器重新初始化为1。遍历结束时,函数返回max的值。

程序运行结果示例1:

Input a string:

55↙

5:5

程序运行结果示例2:

Input a string:

sgf222257↙

2:4

输入提示信息:\"\"Input a string:\\n\"\"

输入格式: 用gets()输入字符串

输出格式:\"\"%c:%d\\n\"\"

*/

#include <>

#include <>

int main()

{

char a[80];

int b, i,j,t=1,tl,num=0;

printf(\"\"Input a string:\\n\"\");

gets(a);

for (i=0;it=1;

for (j=i+1;jif (a[j]==a[i]){

t++;

}

}

if (i==0){

tl=t;

}

else {

if (t>tl){

tl=t;

num=i;

}

}

}

printf(\"\"%c:%d\\n\"\

}\"

\"/*

从键盘输入一串字符(假设字符数少于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

输入提示信息:\"\"Input a string:\"\"

输入格式: \"\"%7s\"\"

输出格式:\"\"%d\\n\"\"

*/

#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(\"\"Input a string:\"\");

scanf(\"\"%7s\"\

printf(\"\"%d\"\

printf(\"\"\\n\"\");

return 0;

}\"

\"/*输入n个整数(n从键盘输入,假设n的值不超过100),按奇偶数分成两组并输出。输出两行,第一行为所有奇数,第二行为所有偶数,保持数据的相对顺序与输入顺序

相同。

函数原型如下所示:

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

printf(\"\"Input final score:\"\");scanf(\"\"%d\"\

printf(\"\"Input

score:\"\");scanf(\"\"%d\"\

class

printf(\"\"Class

not(Y/N):\"\");scanf(\"\"%c\"\

cadre or

printf(\"\"Students from the West or

not(Y/N):\"\");scanf(\"\"%c\"\

printf(\"\"Input the number of published

papers:\"\");scanf(\"\"%d\"\

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].scholarship+=1000;

(stu[i].finalScore>85 && stu[i].west=='Y')

if (stu[i].classScore> 80 && stu[i].work=='Y')

stu[i].scholarship+=850;

printf(\"\"name:%s,scholarship:%d\\n\"\

}

int ts=stu[0].scholarship,k;

for (i=1;iif (tsk=i;

}

}

printf(\"\"%s get the highest scholarship %d\\n\"\

/*

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

游戏开始提示信息:\"\"Game start!\\n\"\"

\"\"Note: the maximum number is 3\\n\"\"

提示游戏者输入移动的火柴数:\"\"Please enter the number of matches you are moving:\\n\"\"

游戏者输入错误数据的提示信息:\"\"The number you entered is wrong,please re-enter!\\n\"\"

输入格式: \"\"%d\"\"

输出格式:

输出被游戏者移动的火柴数:\"\"The number of matches you are moving is:%d\\n\"\"

输出被计算机移动的火柴数:\"\"The number of matches that have been moved by the computer is:%d\\n\"\"

输出被游戏者或计算机移动后剩余的火柴数:\"\"The number of matches left

is:%d\\n\"\"

游戏者获胜的输出提示信息:\"\"Congratulations!You won!\\n\"\"

游戏者失败的输出提示信息:\"\"I'm sorry. You lost!\\n\"\"

*/

#include <>

main()

{

printf(\"\"Game start!\\nNote: the maximum number is 3\\n\"\");

int i, j, k, s = 23, n, c;

while (1)

{

printf(\"\"Please enter the number of matches moving:\\n\"\");

scanf(\"\"%d\"\

you are

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

;

}

else{

printf(\"\"The number you entered is wrong,please re-enter!\\n\"\");

continue;

}

s -= n;

printf(\"\"The number of matches you are moving is:%d\\nThe number 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 number of matches that have been moved by the computer is:%d\\nThe number of matches left is:%d\\n\"\

if (s==0){

printf(\"\"Congratulations!You won!\\n\"\");

break;

}

}

else if (s==0){

printf(\"\"I'm sorry. You lost!\\n\"\");break;

}

}

}\"

\"/*

题目内容:请输入星期几的第一个字母(不区分大小写)来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母(小写),否则输出“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

第一个字母的输入提示信息:\"\"please input the first letter of someday:\\n\"\"

第二个字母的输入提示信息:\"\"please input second letter:\\n\"\"

用户输入错误提示信息:\"\"data error\\n\"\"

输入格式: \"\" %c\"\" (注意:%c前面有一个空格)

输出格式:

星期一:\"\"monday\\n\"\"

星期二:\"\"tuesday\\n\"\"

星期三:\"\"wednesday\\n\"\"

星期四:\"\"thursday\\n\"\"

星期五:\"\"friday\\n\"\"

星期六:\"\"saturday\\n\"\"

星期日:\"\"sunday\\n\"\"

*/

#include<>

main()

{

char i,j;

printf(\"\"please input the first letter of someday:\\n\"\");

scanf(\"\"%c\"\

switch(i)

{

case 'm':case 'M':

printf(\"\"monday\\n\"\");

break;

case 'w':case 'W':

printf(\"\"wednesday\\n\"\");

break;

case 'f':case 'F':

printf(\"\"friday\\n\"\");

break;

case 't':case 'T':

printf(\"\"please input second letter:\\n\"\");

scanf(\"\"%c\"\

j=getchar();

if (j=='u')

printf(\"\"tuesday\\n\"\");

else if (j=='h')

printf(\"\"thursday\\n\"\");

else printf(\"\"data error\\n\"\");

break;

case 's':case 'S':

printf(\"\"please input second letter:\\n\"\");

scanf(\"\"%c\"\

j=getchar();

if (j=='a')

printf(\"\"saturday\\n\"\");

else if (j=='u')

printf(\"\"sunday\\n\"\");

else printf(\"\"data error\\n\"\");

break;

default : printf(\"\"data error\\n\"\"); break;

}

}\"

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igat.cn 版权所有 赣ICP备2024042791号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务