A.Changing Volume

题目:改变音量##

题意:给定两个数a和b,有6个操作(-5, -2, -1, +1, +2, +5),求a变到b的最小操作次数
操作的过程中不能变到小于0,即音量不能调到小于0

分析:
(贪心),我们可以不断使用+5,直到a和b的差值小于5,然后再使用-2,-1,+1,+2这些操作
因为如果a和b的差值大于5,显然使用+2,+2,+1的三次操作可以通过+5一次操作代替

#include <cstdio>
#include <algorithm>

using namespace std;
int t;
int main()
{
	scanf("%d", &t);

	while (t--)
	{
		int a, b;
		scanf("%d%d", &a, &b);
		if (a > b)
			swap(a, b);

		int ans = 0;
		ans = ans + (b - a) / 5;
		a = a + (b - a) / 5 * 5;
		ans = ans + (b - a) / 2;
		a = a + (b - a) / 2 * 2;
		ans = ans + (b - a);
		printf("%d
", ans);
	}


	return 0;
}
原文地址:https://www.cnblogs.com/pixel-Teee/p/11962592.html