Codeforces Round #535 (Div. 3) B. Divisors of Two Integers(思维)

版权声明:欢迎转载,若转载,请标明出处,如有错误,请指点,也欢迎大佬们给出优化方法 https://blog.csdn.net/Charles_Zaqdt/article/details/86623468

题目链接:http://codeforces.com/contest/1108/problem/B

       题意是给了n个数,让找出两个数x和y,使得这n个数中都为x或y的因子。

       思路也很简单,最大值一定是一个解,然后我们先把最大值分解了以后,在剩下的序列中找最大的不是最大值的因子的数就好了。


AC代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
map<int,int> m;

int main()
{
  int n;
	cin>>n;
	int pre[205];
	int a = 0;
	int b = 0;
	for(int i=0;i<n;i++){
		scanf("%d",&pre[i]);
		a = max(a, pre[i]);
		m[pre[i]] ++;
	}
	for(int i=1;i<=a;i++){
		if(a % i == 0){
			m[i] --;
		}
	}
	for(int i=a;i>=1;i--){
		if(m[i] != 0){
			b = i;
			break;
		}
	}
	printf("%d %d\n",a, b);
  return 0;
}

猜你喜欢

转载自blog.csdn.net/Charles_Zaqdt/article/details/86623468