P1909 题解

题面地址在洛谷,自行搜索(懒得打了:D)

本题是一道模拟题,可直接计算出答案,测试点比较多,说一下核心思路。
题面中提到了,只会买同一种的铅笔,那么只需要把三个组合的总价分别算出再比较就可以了。计算公式如下:

\[Ans_0 = \frac{n}{q}\times{p} \]

若n mod q != 0,则是这样计算:

\[Ans_1 = \frac{n + 1}{q}\times{p} \]

代码如下,二十个点已测试通过,请放心食用(C++):

#include <iostream>

int main()
{
	int n;
	std::cin >> n;
	
	int q1,p1,q2,p2,q3,p3;
	std::cin >> q1 >> p1 >> q2 >> p2 >> q3 >> p3;
	
	// solution 1
	int amount1 = n%q1==0 ? (n/q1)*p1 : ((n/q1)+1)*p1;
	// solution 2
	int amount2 = n%q2==0 ? (n/q2)*p2 : ((n/q2)+1)*p2;
	// solution 3
	int amount3 = n%q3==0 ? (n/q3)*p3 : ((n/q3)+1)*p3;
	
	if (amount1 < amount2 && amount1 < amount3) std::cout << amount1 << std::endl;
	else if (amount2 < amount1 && amount2 <= amount3) std::cout << amount2 << std::endl;
	else if (amount3 < amount1 && amount3 <= amount2) std::cout << amount3 << std::endl;
	
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/kozumi/p/12903667.html