题解 | #最大公约数1#

最大公约数1

https://www.nowcoder.com/practice/021010dda9f04900a86738931a5600a4

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main(){
	int n;
	int a[100000000];
	while( cin >> n ){
		for(int i=0; i<n; i++)
			cin >> a[i];
		sort(a, a+n);
		int min = a[0];
		int max = a[n-1];
		vector<int> v1;
		vector<int> v2;
		vector<int> v;
		for(int i=1; i*i<=min+10; i++){
			if( min % i == 0 )
				v1.insert(v1.begin(), i);
		}
		v1.insert(v1.begin(),min);
		for(int i=1; i*i<=max+10; i++){
			if( max % i == 0 )
				v2.insert(v2.begin(), i);
		}
		v2.insert(v2.begin(),max);
		cout << min << " "<< max << " " ;
		for(int i=0; i<v1.size(); i++){
			for(int j=0; j<v2.size(); j++){
				if( v1[i] == v2[j] )
					v.push_back(v1[i]);
			}
		}
		sort(v.begin(), v.end());
		cout << v[v.size()-1] << endl;
//		cout << min << " " << max;
		
	}

	return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务