题目
Snuke had N cards numbered 1 through N. Each card has an integer written on it; written on Card i is ai.
Snuke did the following procedure:
Let X and x be the maximum and minimum values written on Snuke’s cards, respectively.
1.If X=x, terminate the procedure. Otherwise, replace each card on which X is written with a card on which X−x is written, then go back to step 1.
2.Under the constraints in this problem, it can be proved that the procedure will eventually terminate. Find the number written on all of Snuke’s cards after the procedure.
Constraints
All values in input are integers.
1≤N≤105
1≤ai≤109
输入
Input is given from Standard Input in the following format:
N
a1 a2 ⋯ aN
输出
Print the number written on all of Snuke’s cards after the procedure.
样例输入
【样例1】
3
2 6 6
【样例2】
15
546 3192 1932 630 2100 4116 3906 3234 1302 1806 3528 3780 252 1008 588
样例输出
【样例1】
2
【样例2】
42
题意
输入一队数组,找到最大的数(X)和最小的数(x),if(X!=x) X-=x;
然后在这样找一直到X==x并输出值;
思路
就是找这队数组的最大公约数gcd
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int a[N];
int gcd(int a,int b){
return b>0 ? gcd(b,a%b):a;
}
int main(){
int n,X=-99999999,x=9999999,i;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i];
}
int kgcd=gcd(a[1],a[2]);
for(int i=2;i<=n;i++){
kgcd=gcd(kgcd,a[i]);
}
cout<<kgcd;
return 0;
}