本文主要是介绍问题 B: Scaling Recipe,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目描述
You’ve got a recipe which specifies a number of ingredients, the amount of each ingredient you will need, and the number of portions it produces. But, the number of portions you need is not the same as the number of portions specified in the recipe! How can you scale it?
翻译
你有一个食谱,其中指定了一些食材,你需要的每种食材的数量,以及它产生的分量。但是,你需要的份量和食谱中指定的份量不一样!你如何衡量它?
输入
The first line of input contains three integers n (1 ≤ n ≤ 40), x and y (1 ≤ x, y ≤ 40,000), where n is the number of ingredients in the recipe, x is the number of portions that the recipe produces, and y is the number of portions you need. Each of the next n lines contains a single integer a (1 ≤ a ≤ 40,000). These are the amounts of each ingredient needed for the recipe. The inputs will be chosen so that the amount of each ingredient needed for y portions will be an integer.
翻译
输入的第一行包含三个整数n(1≤n≤40),x和y(1≤x, y≤40000),其中n是配方成分的数量,x是部分配方生产的数量,和y是部分你需要的数量。后面的每n行包含一个整数a(1≤a≤40000)。这是食谱中每种原料的用量。将选择输入,以便y部分所需的每个成分的数量将是一个整数。
输出
Output n lines. On each line output a single integer, which is the amount of that ingredient needed to produce y portions of the recipe. Output these values in the order of the input.
翻译
输出n行。在每一行上输出一个整数,这是生成recipe的y部分所需的配料的数量。按输入的顺序输出这些值。
PS:关键点,等比例缩放,并且数值不能爆炸
代码:
#include<iostream>
using namespace std;int gcd(int m, int n) {//求公因数函数while (m != n) {if (m > n)m = m - n;elsen = n - m;}return n;
}
int main()
{int n, x, y, a, t;cin >> n >> x >> y;t = gcd(x, y);x = x / t;y = y / t;for (int i = 0; i < n; i++) {cin >> a;int z = gcd(x, a), temp;a = a / z;temp = x / z;cout << (y * a) / temp << endl;}return 0;
}
这篇关于问题 B: Scaling Recipe的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!