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

codechef Subtraction Game 1题解

时间:2014-05-07 03:14:01      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   ext   

Subtraction Game 1

Chef is playing a game on a sequence of N positive integers, say A1, A2, ... AN. The game is played as follows.

  • If all the numbers are equal, the game ends.
  • Otherwise
    • Select two numbers which are unequal
    • Subtract the smaller number from the larger number
    • Replace the larger number with the result from above (see the explanation section for clarity)

Chef has already figured out that the game always terminates. He also knows, for a given sequence of integers, the game will always terminate on the same value, no matter how the game is played. Chef wants you to simulate the game for him and tell him on which value will the game terminate for a given sequence of integers.

Input

The first line of the input contains an integer T, the number of test cases. Then follow the description of T test cases. The first line of each test case contains a single integer N, the length of the sequence. The second line contains N positive integers, each separated by a single space.

Output

For each test case, output a single integer - the value of all the numbers when they are equal (and the game terminates), on a line by itself.

Constraints

1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ Ai ≤ 109

Sample

Input
3
2
10 12
2
5 9
3
6 10 15

Output
2
1
1

可以这么来玩游戏的。

不过本题实质就是求一个数列的最大公约数。

#pragma once
#include <stdio.h>
#include <stdlib.h>

class SubtractionGame1
{
	int GCD(int a, int b)
	{
		if (0 == b) return a;
		return GCD(b, a % b);
	}

public:
	void run()
	{
		int T = 0, N = 0;
		scanf("%d", &T);
		while (T--)
		{
			scanf("%d", &N);
			if (N < 1) continue;

			int ans = 0;
			scanf("%d", &ans);
			while (--N)
			{
				int b = 0;
				scanf("%d", &b);
				ans = GCD(b, ans);
			}
			printf("%d\n", ans);
		}
	}
};

int subtractionGame1()
{
	SubtractionGame1 su;
	su.run();
	return 0;
}



codechef Subtraction Game 1题解,布布扣,bubuko.com

codechef Subtraction Game 1题解

标签:des   style   blog   class   code   ext   

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

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