MAX-=min

题目

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;
} 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nap!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值