本博客内容为中国大学生MOOC国家精品课程《计算机程序设计C++》作业记录,仅供参考,观者忌照搬照抄,欢迎交流批评指正!
(注:由于本人学习时,前八周的作业提交时间已过,因此这八周的作业代码只在自己的编译器上测试运行通过,在课程网站上还未测试,于下学期开课时,再另行测试,如您发现有明显错误,可留言评论)
##第3周编程作业
本周作业内容C++流程的控制
- 打印3个相邻字母
#include<iostream>
using namespace std;
int main()
{
char b,a,c;
cin >> b;
a = b - 1;
c = b + 1;
if (a == ('A' - 1))a = 'Z';
if (a == ('a' - 1))a = 'z';
if (c == ('Z' + 1))c = 'A';
if (c == ('z' + 1))c = 'a';
cout << a << b << c;
return 0;
}
- 歌唱大赛选手成绩计算
#include<iostream>
using namespace std;
int main()
{
double score[10];
double max=0, min=100, avg=0;
bool judge = true;
int i = 0;
for (i = 0; i < 10; i++)
{
cin >> score[i];
max = (max > score[i]) ? max : score[i];
min = (min < score[i]) ? min : score[i];
avg += score[i];
if (score[i] > 100 || score < 0) judge = false;
}
if (!judge)
{
cout << "the score is invalid.";
return 0;//注意不能返回非0,不然会出现网站无法通过
}
avg -= (min + max);
avg /= 8.0;
cout << avg;
return 0;
}
- 猴子吃桃
#include<iostream>
using namespace std;
int main()
{
int n,sum=1;
cin >> n;
for (int i = 0; i < n - 1; i++)
{
sum = (sum + 1) * 2;
}
cout << sum;
}
- 搬砖问题
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int man = 0, woman = 0, child = 0;
bool judge = false;
while (1)
{
if ((n - 7 * man) / 5.0 == (n - 7 * man) / 5)
{
woman = (n - 7 * man)/5.0;
child = n - man - woman;
cout << "men" << man << endl;
cout << "women" << woman << endl;
cout << "children" << child << endl;
judge = true;
}
man++;
if (man > n / 7)break;
}
if (!judge) cout << "no result!";
return 0;
}
- 美分找钱
#include<iostream>
using namespace std;
int main()
{
int money,face[4] = { 25, 10, 5, 1 },count=0;
cin >> money;
if (money < 0 || money>100)
{
cout << "the money is invalid!";
return 0;
}
for (int i = 0; i <= money; i++)
{
for (int j = 0; j <= money / 5; j++)
{
for (int k = 0; k <= money / 10; k++)
{
for (int l = 0; l <= money / 25; l++)
{
if (i + 5 * j + 10 * k + l*25 == money)
{
count++;
break;
}
}
}
}
}
cout << count;
return 0;
}
以上为第3周编程作业。