ZOJ 3844 暴力
---恢復內容開始---
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
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 }
ZOJ 3844 暴力