哈工大 C语言程序设计精髓 MOOC慕课 6-12周编程题答案 下载本文

内容发布更新时间 : 2024/6/20 20:07:11星期一 下面是文章的全部内容请认真阅读。

while(n!=0) {

b=n; n/=10; a[i]=b; i++; }

a[i]=n;

int flag=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: Input a string: 55↙ 5:5

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

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

#include <> #include <>

int main() {

char a[80];

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

printf(\

gets(a);

for (i=0;i

for (j=i+1;j

if (i==0){ tl=t; }

else {

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(\final score:\ printf(\class score:\ printf(\cadre or not?(Y/N):\ printf(\from the West or not?(Y/N):\ printf(\the number of published 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

游戏开始提示信息:\

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