哈工大c语言程序设计精髓mooc慕课6-12周编程题答案_第1页
哈工大c语言程序设计精髓mooc慕课6-12周编程题答案_第2页
哈工大c语言程序设计精髓mooc慕课6-12周编程题答案_第3页
哈工大c语言程序设计精髓mooc慕课6-12周编程题答案_第4页
哈工大c语言程序设计精髓mooc慕课6-12周编程题答案_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

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

文档简介

*/

#include<>

#include<>

intmain()

{

charscore[100];

intflag=0,i,s;

chargrade;

while(1)

{

flag=0;

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)

{

continue;

}

else{

break;

}

}

s=atoi(score);

if(s>=90)

{

grade='A';

}

elseif(s>=

{

grade

}

elseif(s>=

{

grade

}

elseif(s>=

{

grade

}

else

{

grade

}

80)

='B';

70)

='C';

60)

='D';

='E';

return0;

#include<>

intmain()

{

intn,a,i,j;

doublep=0,q=0;

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

{

for(j=0,p=0;j<i;j++)

{

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

}

q=p+q;

}

return0;

n块砖(27<n<=77),36人搬,男搬4,女搬3,两个小孩抬一块砖,要求一次搬完,问男

人、女人和小孩各需多少人?请用穷举法编程求解,n的值要求从键盘输入。输出结果按照

男人数量升序给出(见下面示例3)

程序的运行结果示例1:

Inputn(27<n<=77):

28↙

men=0,women=4,children=32

程序的运行结果示例2:

Inputn(27<n<=77):

36↙

men=3,women=3,children=30

程序的运行结果示例3:

Inputn(27<n<=77):

60↙

men=2,women=14,children=20

men=7,women=7,children=22

men=12,women=0,children=24

输入提示:

输入格式

输出格式:

*/

main()

{

longn,i,t,s=0;

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)

{

}

intmain()

{intyear,month,day;

switch(month)

{

case

case

case

case

case

case

case

case

case

case

case

case

1:day=31;break;

2:day=28;break;

3:day=31;break;

4:day=30;break;

5:day=31;break;

6:day=30;break;

7:day=31;break;

8:day=31;break;

9:day=30;break;

10:day=31;break;

11:day=30;break;

12:day=31;break;

}

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

if(day!=-1)

return0;

unsignedintComputeAge(unsignedintn){

}

main()

{

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

intgys(inta,intb)

{

intr;

r=a%b;

if(r==0)returnb;

elsereturngys(b,r);

}

main()

{

a[i]=b;

i++;

}

a[i]=n;

intflag=0;

for(c=0;c<i;c++)

for(d=c+1;d<i;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

输入提示信息:

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

输出格式:

*/

#include<>

#include<>

intmain()

{

chara[80];

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

gets(a);

for(i=0;i<strlen(a);i++){

t=1;

for(j=i+1;j<strlen(a);j++){

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

t++;

}

}

if(i==0){

tl=t;

}

else{

if(t>tl){

tl=t;

num=i;

}

}

}

从键盘输入一串字符(假设字符数少于8个)

,以回车表示输入结束,编程将其中的数字部

分转换为整型数并以整型的形式输出。

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

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

解题思路的关键是:1)判断字符串中的字符是否是数字字符;2)如何将数字字符转换为其

对应的数字值;3)如何将每一个转换后的数字值加起来形成一个整型数。

程序运行结果示例1:

Inputastring:7hg09y↙

709

程序运行结果示例2:

Inputastring:9w2k7m0↙

9270

程序运行结果示例3:

Inputastring:happy↙

0

输入提示信息:

输入格式

输出格式:

*/

#include<>

#include<>

#include<>

intMyatoi(charstr[]){

inti,j;

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

str[j]=str[i];

j++;

}

}

returnatoi(str);

}

intmain()

{

chars[7];

return0;

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

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

函数原型如下所示:

voidSeperate(inta[],intn);ame);

final

class

cadre

from

the

the

number

or

West

of

or

published

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;

}

intts=stu[0].scholarship,k;

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

if(ts<stu[i].scholarship){

k=i;

}

}

/*

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

Thenumberofmatchesthathavebeenmoved

Thenumberofmatchesleftis:14

Pleaseenterthenumberofmatchesyouare

2↙

Thenumberofmatchesyouaremovingis:2

Thenumberofmatchesleftis:12

Thenumberofmatchesthathavebeenmoved

Thenumberofmatchesleftis:11

Pleaseenterthenumberofmatchesyouare

3↙

Thenumberofmatchesyouaremovingis:3

Thenumberofmatchesleftis:8

Thenumberofmatchesthathavebeenmoved

Thenumberofmatchesleftis:5

Pleaseenterthenumberofmatchesyouare

1↙

Thenumberofmatchesyouaremovingis:1

Thenumberofmatchesleftis:4

Thenumberofmatchesthathavebeenmoved

Thenumberofmatchesleftis:2

Pleaseenterthenumberofmatchesyouare

1↙

Thenumberofmatchesyouaremovingis:1

Thenumberofmatchesleftis:1

Thenumberofmatchesthathavebeenmoved

Thenumberofmatchesleftis:0

Congratulations!Youwon!

程序运行结果示例2:

3

3

2

1

3

1

bythecomputeris:2

moving:

bythecomputeris:1

moving:

bythecomputeris:3

moving:

bythecomputeris:2

moving:

bythecomputeris:1

游戏开始提示信息:

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

enterthenumberofmatchesyouare

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

numberyouenterediswrong,please

输入格式

输出格式:

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

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

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

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

游戏者失败的输

温馨提示

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

评论

0/150

提交评论