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

上传人:b****7 文档编号:15948620 上传时间:2023-07-09 格式:DOCX 页数:21 大小:19.02KB
下载 相关 举报
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第1页
第1页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第2页
第2页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第3页
第3页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第4页
第4页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第5页
第5页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第6页
第6页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第7页
第7页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第8页
第8页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第9页
第9页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第10页
第10页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第11页
第11页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第12页
第12页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第13页
第13页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第14页
第14页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第15页
第15页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第16页
第16页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第17页
第17页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第18页
第18页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第19页
第19页 / 共21页
哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx_第20页
第20页 / 共21页
亲,该文档总共21页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

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

《哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx》由会员分享,可在线阅读,更多相关《哈工大 C语言程序设计精髓 MOOC慕课 612周编程题答案.docx(21页珍藏版)》请在冰点文库上搜索。

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

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

"/*下面代码的功能是将百分制成绩转换为5分制成绩,具体功能是:

如果用户输入的是非法字符或者不在合理区间内的数据(例如输入的是a,或者102,或-45等),则程序输出Inputerror!

,并允许用户重新输入,直到输入合法数据为止,并将其转换为5分制输出。

目前程序存在错误,请将其修改正确。

并按照下面给出的运行示例检查程序。

*/

#include<>

#include<>

intmain()

{

charscore[100];

intflag=0,i,s;

chargrade;

printf(""Pleaseinputscore:

\n"");

while

(1)

{

flag=0;

scanf(""%s"",score);

for(i=0;i

{

if(score[i]>='0'&&score[i]<='9')

{

continue;

}

else

{

flag=1;

break;

}

}

s=atoi(score);

if(s<0||s>100||flag==1)

{

printf(""Inputerror!

\n"");

printf(""Pleaseinputscore:

\n"");

continue;

}

else{

break;

}

}

s=atoi(score);

if(s>=90)

{

grade='A';

}

elseif(s>=80)

{

grade='B';

}

elseif(s>=70)

{

grade='C';

}

elseif(s>=60)

{

grade='D';

}

else

{

grade='E';

}

printf(""grade:

%c\n"",grade);

return0;

}"

"#include<>

#include<>

intmain()

{

intn,a,i,j;

doublep=0,q=0;

printf(""Inputa,n:

\n"");

scanf(""%d,%d"",&a,&n);

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"",q);

return0;

}"

"/*

n块砖(27

请用穷举法编程求解,n的值要求从键盘输入。

输出结果按照男人数量升序给出(见下面示例3)。

程序的运行结果示例1:

Inputn(27

28↙

men=0,women=4,children=32

程序的运行结果示例2:

Inputn(27

36↙

men=3,women=3,children=30

程序的运行结果示例3:

Inputn(27

60↙

men=2,women=14,children=20

men=7,women=7,children=22

men=12,women=0,children=24

输入提示:

""Inputn(27

\n""

输入格式:

""%d""

输出格式:

""men=%d,women=%d,children=%d\n""

*/

#include""""

main()

{

printf(""Inputn(27

\n"");

longn,i,t,s=0;

scanf(""%d"",&n);

inta,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"",a,b,c);

}

}"

"#include<>

intmain()

{intyear,month,day;

printf(""Inputyear,month:

\n"");

scanf(""%d,%d"",&year,&month);

switch(month)

{

case1:

day=31;break;

case2:

day=28;break;

case3:

day=31;break;

case4:

day=30;break;

case5:

day=31;break;

case6:

day=30;break;

case7:

day=31;break;

case8:

day=31;break;

case9:

day=30;break;

case10:

day=31;break;

case11:

day=30;break;

case12:

day=31;break;

default:

day=-1;printf(""Inputerror!

\n"");

}

if((year%4==0&&year%100!

=0||year%400==0)&&month==2)day=29;

if(day!

=-1)

printf(""%ddays\n"",day);

return0;

}"

"#include<>

unsignedintComputeAge(unsignedintn){

}

main()

{

inti,j,k,s=23,n,c,age;

scanf(""%d"",&n);

printf(""Theperson'sageis%u\n"",8+2*n);

}"

"#include<>

intgys(inta,intb)

{

intr;

r=a%b;

if(r==0)returnb;

elsereturngys(b,r);

}

main()

{

printf(""Inputa,b:

"");

inta,b;

scanf(""%d,%d"",&a,&b);

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

printf(""Inputerror!

\n"");

}

else

printf(""%d\n"",gys(a,b));

}"

"#include<>

intmedian(inta,intb,intc)

{

if(a

{

if(b

else{returna

c:

a;}程序运行结果示例1:

Inputn:

28212

Repeateddigit!

程序运行结果示例2:

Inputn:

12345↙

Norepeateddigit!

输入提示:

""Inputn:

\n""

输入格式:

""%ld""

输出格式:

有重复数字,输出信息:

""Repeateddigit!

\n""

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

""Norepeateddigit!

\n""

*/

#include<>

intmain()

{

intlog[10]={0},a[100];

intb,i=0,n,c,d;

printf(""Inputn:

\n"");

scanf(""%d"",&n);

while(n!

=0)

{

b=n%10;

n/=10;

a[i]=b;

i++;

}

a[i]=n;

intflag=0;

for(c=0;c

for(d=c+1;d

{

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

{

flag=1;

break;

}

}

str[i]!

=str[i+1],则计数器重新初始化为1。

遍历结束时,函数返回max的值。

程序运行结果示例1:

Inputastring:

55↙

5:

5

程序运行结果示例2:

Inputastring:

sgf222257↙

2:

4

输入提示信息:

""Inputastring:

\n""

输入格式:

用gets()输入字符串

输出格式:

""%c:

%d\n""

*/

#include<>

#include<>

intmain()

{

chara[80];

intb,i,j,t=1,tl,num=0;

printf(""Inputastring:

\n"");

gets(a);

for(i=0;i

t=1;

for(j=i+1;j

if(a[j]==a[i]){

t++;

}

}

if(i==0){

tl=t;

}

else{

if(t>tl){

tl=t;

num=i;

}

}

}

printf(""%c:

%d\n"",a[num],tl);

}"

"/*

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

函数原型为intMyatoi(charstr[]);

其中,形参数组str[]对应用户输入的字符串,函数返回值为转换后的整型数。

解题思路的关键是:

1)判断字符串中的字符是否是数字字符;2)如何将数字字符转换为其对应的数字值;3)如何将每一个转换后的数字值加起来形成一个整型数。

程序运行结果示例1:

Inputastring:

7hg09y↙

709

程序运行结果示例2:

Inputastring:

9w2k7m0↙

9270

程序运行结果示例3:

Inputastring:

happy↙

0

输入提示信息:

""Inputastring:

""

输入格式:

""%7s""

输出格式:

""%d\n""

*/

#include<>

#include<>

#include<>

intMyatoi(charstr[]){

inti,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';

returnatoi(str);

}

intmain()

{

chars[7];

printf(""Inputastring:

"");

scanf(""%7s"",s);

printf(""%d"",Myatoi(s));

printf(""\n"");

return0;

}"

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

输出两行,第一行为所有奇数,第二行为所有偶数,保持数据的相对顺序与输入顺序相同。

函数原型如下所示:

voidSeperate(inta[],intn);?

ame);

printf(""Inputfinalscore:

"");scanf(""%d"",&stu[i].finalScore);

printf(""Inputclassscore:

"");scanf(""%d"",&stu[i].classScore);getchar();

printf(""Classcadreornot?

(Y/N):

"");scanf(""%c"",&stu[i].work);getchar();

printf(""StudentsfromtheWestornot?

(Y/N):

"");scanf(""%c"",&stu[i].west);getchar();

printf(""Inputthenumberofpublishedpapers:

"");scanf(""%d"",&stu[i].paper);

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(""name:

%s,scholarship:

%d\n"",stu[i].name,stu[i].scholarship);

}

intts=stu[0].scholarship,k;

for(i=1;i

if(ts

k=i;

}

}

printf(""%sgetthehighestscholarship%d\n"",stu[k].name,stu[k].scholarship);

/*

1)院士奖学金:

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

2)五四奖学金:

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

3)成绩优秀奖:

期末平均成绩高于90分(>90)的学生每人均可获得2000元;

4)西部奖学金:

期末平均成绩高于85分(>85)的西部省份学生每人均可获得1000元;

5)班级贡献奖:

班级评议成绩高于80分(>80)的学生干部每人均可获得850元;

*/

return0;

}"

"/*

请编写一个简单的23根火柴游戏程序,实现人跟计算机玩这个游戏的程序。

为了方便程序自动评测,假设计算机移动的火柴数不是随机的,而是将剩余的火柴根数对3求余后再加1来作为计算机每次取走的火柴数。

如果计算机打算移走的火柴数等于剩下的火柴数,则将计算机打算移走的火柴数减1。

但是计算机不可以不取,剩下的火柴数为1时,必须取走1根火柴。

假设游戏规则如下:

1)游戏者开始拥有23根火柴棒;

2)每个游戏者轮流移走1根、2根或3根火柴;

3)谁取走最后一根火柴为失败者。

程序运行结果示例1:

Gamestart!

Note:

themaximumnumberis3

Pleaseenterthenumberofmatchesyouaremoving:

5↙

Thenumberyouenterediswrong,pleasere-enter!

Pleaseenterthenumberofmatchesyouaremoving:

3↙

Thenumberofmatchesyouaremovingis:

3

Thenumberofmatchesleftis:

20

Thenumberofmatchesthathavebeenmovedbythecomputeris:

3

Thenumberofmatchesleftis:

17

Pleaseenterthenumberofmatchesyouaremoving:

1↙

Thenumberofmatchesyouaremovingis:

1

Thenumberofmatchesleftis:

16

Thenumberofmatchesthathavebeenmovedbythecomputeris:

2

Thenumberofmatchesleftis:

14

Pleaseenterthenumberofmatchesyouaremoving:

2↙

Thenumberofmatchesyouaremovingis:

2

Thenumberofmatchesleftis:

12

Thenumberofmatchesthathavebeenmovedbythecomputeris:

1

Thenumberofmatchesleftis:

11

Pleaseenterthenumberofmatchesyouaremoving:

3↙

Thenumberofmatchesyouaremovingis:

3

Thenumberofmatchesleftis:

8

Thenumberofmatchesthathavebeenmovedbythecomputeris:

3

Thenumberofmatchesleftis:

5

Pleaseenterthenumberofmatchesyouaremoving:

1↙

Thenumberofmatchesyouaremovingis:

1

Thenumberofmatchesleftis:

4

Thenumberofmatchesthathavebeenmovedbythecomputeris:

2

Thenumberofmatchesleftis:

2

Pleaseenterthenumberofmatchesyouaremoving:

1↙

Thenumberofmatchesyouaremovingis:

1

Thenumberofmatchesleftis:

1

Thenumberofmatchesthathavebeenmovedbythecomputeris:

1

Thenumberofmatchesleftis:

0

Congratulations!

Youwon!

程序运行结果示例2:

3

3

2

1

3

1

游戏开始提示信息:

""Gamestart!

\n""

""Note:

themaximumnumberis3\n""

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

""Pleaseenterthenumberofmatchesyouaremoving:

\n""

游戏者输入错误数据的提示信息:

""Thenumberyouenterediswrong,pleasere-enter!

\n""

输入格式:

""%d""

输出格式:

输出被游戏者移动的火柴数:

""Thenumberofmatchesyouaremovingis:

%d\n""

输出被计算机移动的火柴数:

""Thenumberofmatchesthathavebeenmovedbythecomputeris:

%d\n""

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

""Thenumberofmatchesleftis:

%d\n""

游戏者获胜的输出提示信息:

""Congratulations!

Youwon!

\n""

游戏者失败的输出提示信息:

""I'msorry.Youlost!

\n""

*/

#include<>

main()

{

printf(""Gamestart!

\nNote:

themaximumnumberis3\n"");

inti,j,k,s=23,n,c;

while

(1)

{

printf(""Pleaseenterthenumberofmatchesyouaremoving:

\n"");

scanf(""%d"",&n);

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

;

}

else{

printf(""Thenumberyouenterediswrong,pleasere-enter!

\n"");

continue;

}

s-=n;

printf(""Thenumberofmatchesyouaremovingis:

%d\nThenumberofmatchesleftis:

%d\n"",n,s);

if(s>0)

{

if(s==3)

{

c=2;

}

elseif(s==2){

c=1;

}

elseif(s==1){

c=1;

}

else

{

c=s%3+1;

}

s-=c;

printf(""Thenumberofmatchesthathavebeenmovedbythecomputeris:

%d\nThenumberofmatchesleftis:

%d\n"",c,s);

if(s==0){

printf(""Congratulations!

Youwon!

\n"");

break;

}

}

elseif(s==0){

printf(""I'msorry.Youlost!

\n"");break;

}

}

}"

"/*

题目内容:

请输入星期几的第一个字母(不区分大小写)来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母(小写),否则输出“dataerror”。

程序运行结果示例1:

pleaseinputthefirstletterofsomeday:

S↙

pleaseinputsecondletter:

u↙

sunday

程序运行结果示例2:

pleaseinputthefirstletterofsomeday:

F↙

friday

程序运行结果示例2:

pleaseinputthefirstletterofsomeday:

h↙

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2