原题如下:
Description
You are given n integers. Your task is very easy. You should find the maximum integer a and the minimum integer b among these nintegers. And then you should replace both a and b with a-b. Your task will not be finished unless all the integers are equal.
Now the problem come, you want to know whether you can finish you task. And if you can finish the task, you want to know the final result.
Input
The first line of the input contain an integer T(T≤ 20) indicates the number of test cases.
Then T cases come. Each case consists of two lines. The first line is an integer n(2≤ n≤ 10) as the problem described. The second line contains n integers, all of them are no less than -100000 and no more than 100000.
Output
For each case you should print one line. If you can finish your task, you should print one of the n integers. Otherwise, you should print "Nooooooo!"(without quotes).
Sample Input
2 3 1 2 3 2 5 5
Sample Output
2 5
题目大意:n个数,每一次把最大的数b和最小的数a变成b-a,直到所有数都相等。
分析:焦头烂额想了很久都不知道什么时候会无法完成任务。后来看过AC代码才知道。肯定最后可以化成相同的数。粗略分析就是每次把最大的和最小的差距拉小,最后肯定能化成相同的。分情况讨论就是,首先最大的和最小的都是正数的话,肯定在逐渐缩小差距。如果一正一负,则肯定可以在有限的步骤里化为正数。
相同了就是再简单不过的模拟。
代码如下:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
#define INF 1000
#define MAXN 15
int a[MAXN];
int main()
{
int t,n;
cin>>t;
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",a+i);
sort(a,a+n);
while(a[0]!=a[n-1])
{
int temp=a[n-1]-a[0];
a[n-1]=temp;
a[0]=temp;
sort(a,a+n);
}
printf("%d\n",a[0]);
}
}