ZOJ 3844 暴力

---恢复内容开始---

 

Easy Task

Time Limit: 2 Seconds      Memory Limit: 65536 KB

You are given n integers. Your task is very easy. You should find the maximum integer a and the minimum integer b among these n integers. 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个数,每轮将最大的一个数字ai和最小的数字aj都变成ai-aj,问最终能否使所有数字相同,并输出这个数字。
Hint
发现每次操作,最大值和最小值的差是严格减少的,所以有限步内就可以达到最终局面,更准确地说,所有情况都会达到最后所有数字相同的局面。
暴力操作1e6次,若不能达到则认为不行。
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,seg[110];
 4 bool check(){
 5     for(int i=1;i<n;i++) if(seg[i]!=seg[i+1]) return false;
 6     return true;
 7 }
 8 int main(){
 9     int T,max_pos,min_pos;
10     cin>>T;
11     while(T--){
12         cin>>n;
13         max_pos=1,min_pos=1;
14         for(int i=1;i<=n;i++) cin>>seg[i];
15         
16         int tot=0;
17         bool flag=false;
18         while(++tot<=1000000){
19             if(check()){
20                 flag=true;    
21                 break;
22             }
23             for(int i=1;i<=n;i++){
24                 if(seg[max_pos]<seg[i]) max_pos=i;
25                 if(seg[min_pos]>seg[i]) min_pos=i;
26             }
27             seg[max_pos]=seg[min_pos]=(seg[max_pos]-seg[min_pos]);
28         }
29         if(flag) cout<<seg[1]<<endl;
30         else cout<<"Nooooooo!"<<endl;        
31     }
32     return 0;
33 }

 

posted on 2017-07-29 14:50  poler  阅读(170)  评论(0编辑  收藏  举报

导航