码迷,mamicode.com
首页 > 其他好文 > 详细

codechef Cutting Recipes题解

时间:2014-05-04 17:54:01      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   ext   color   

Cutting Recipes

The chef has a recipe he wishes to use for his guests,
but the recipe will make far more food than he can serve to the guests.
The chef therefore would like to make a reduced version of the recipe which has the same ratios of ingredients, but makes less food.
The chef, however, does not like fractions.
The original recipe contains only whole numbers of ingredients,
and the chef wants the reduced recipe to only contain whole numbers of ingredients as well.
Help the chef determine how much of each ingredient to use in order to make as little food as possible.

Input

Input will begin with an integer T, the number of test cases.
Each test case consists of a single line.
The line begins with a positive integer N, the number of ingredients.
N integers follow, each indicating the quantity of a particular ingredient that is used.

Output

For each test case, output exactly N space-separated integers on a line,
giving the quantity of each ingredient that the chef should use in order to make as little food as possible.

Sample Input

3
2 4 4
3 2 3 4
4 3 15 9 6

Sample Output

1 1
2 3 4
1 5 3 2

问题就是:求一个数组的所有数的最大公约数

#pragma once
#include <stdio.h>

namespace cutName
{
	int gcd(int a, int b)
	{
		if (0 == b) return a;
		return gcd(b, a % b);
	}
}

int CuttingRecipes()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		int n = 0;
		scanf("%d", &n);
		if (0 == n) continue;

		int *A = new int[n];
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &A[i]);
		}
		int g = A[0];
		for (int i = 1; i < n; i++)
		{
			g = cutName::gcd(A[i], g);
		}
		for (int i = 0; i < n; i++)
		{
			printf("%d ", A[i] / g);
		}
		putc(‘\n‘, stdout);
		delete [] A;
	}
	return 0;
}




codechef Cutting Recipes题解,布布扣,bubuko.com

codechef Cutting Recipes题解

标签:style   blog   class   code   ext   color   

原文地址:http://blog.csdn.net/kenden23/article/details/24965113

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!