计算机程序设计C++ MOOC(第3周编程作业)

这篇博客记录了《计算机程序设计C++》课程中第3周的编程作业,包括打印相邻字母、歌唱比赛成绩计算、猴子吃桃、搬砖问题和美分找钱等实战题目,旨在提升C++流程控制能力。代码已在个人编译器上测试通过,但未在课程网站上验证,欢迎交流讨论。
摘要由CSDN通过智能技术生成

本博客内容为中国大学生MOOC国家精品课程《计算机程序设计C++》作业记录,仅供参考,观者忌照搬照抄,欢迎交流批评指正!

(注:由于本人学习时,前八周的作业提交时间已过,因此这八周的作业代码只在自己的编译器上测试运行通过,在课程网站上还未测试,于下学期开课时,再另行测试,如您发现有明显错误,可留言评论)

##第3周编程作业

本周作业内容C++流程的控制

  1. 打印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;
}
  1. 歌唱大赛选手成绩计算
    在这里插入图片描述
#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;
}
  1. 猴子吃桃
    在这里插入图片描述
#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;
}
  1. 搬砖问题
    在这里插入图片描述
#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;
}
  1. 美分找钱
    在这里插入图片描述
#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周编程作业。

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值