2023年码蹄杯二赛 买月饼 题型:枚举

46 阅读1分钟

码题集OJ-买月饼 (matiji.net)

#include<bits/stdc++.h>
using namespace std;

int f(int judge,int quan)
{
	int cnt = 0;
	if (judge > quan)
	{
		while (judge != 0)
		{
			judge -= quan;
			cnt++;
		}
	}
	else
	{
		while (quan != 0)
		{
			quan -= judge;
			cnt++;
		}
	}
	return cnt;
}
int main()
{

	int judge, quan; cin >> judge >> quan;
	
	if (judge<10)
	{
		cout<<f(judge,quan);
		return 0;
	}
	else
	{
		judge %= 10;
		cout<<f(judge,quan);
		return 0;
	}
	return 0;
}

image.png

一种巧妙的思想

我们去枚举买的月饼数量,看哪个月饼数量是合法的

如果这个数量的月饼总价钱刚好是10的倍数 或者 除去10之后的余数

刚好是 代金券的整数倍,说明就是合法的,输出当前买的月饼的数量

#include <bits/stdc++.h>
using namespace std;
int n, m; //月饼价格,代金券

int main() {
	cin >> m >> n;
	for (int i = 1;; i++) {
		if ((i * m) % 10 == n || (i * m) % 10 == 0) { //能用代金券买尽 或 能用现金买尽
			cout << i << endl;
			return 0;
		}
	}
	return 0;
}

image.png