1108B - Divisors of Two Integers

B. Divisors of Two Integers
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Recently you have received two positive integer numbers x and y
. You forgot them, but you remembered a shuffled list containing all divisors of x (including 1 and x) and all divisors of y (including 1 and y). If dis a divisor of both numbers x and yat the same time, there are two occurrences of d
in the list.
For example, if x=4and y=6 then the given list can be any permutation of the list [1,2,4,1,2,3,6]. Some of the possible lists are: [1,1,2,4,6,3,2],[4,6,1,1,2,3,2] or [1,6,3,2,4,1,2].Your problem is to restore suitable positive integer numbers x and y that would yield the same list of divisors (possibly in different order).It is guaranteed that the answer exists, i.e. the given list of divisors corresponds to some positive integers x and y.
Input
The first line contains one integer n
(2≤n≤128) — the number of divisors of x and y

.The second line of the input contains n integers d1,d2,…,dnd1,d2,…,d
n (1≤di≤1041≤di≤104

), where di is either divisor of x

or divisor of y

. If a number is divisor of both numbers xx

and yy

then there are two copies of this number in the list.OutputPrint two positive integer numbers xx

and y

— such numbers that merged list of their divisors is the permutation of the given list of integers. It is guaranteed that the answer exists.
Example
Input
10
10 2 8 1 2 4 1 20 4 5
Output
20 8
题意:找x和y,x和y所在的数组都是x或y的因数,所以x或y中必有一个是最大的,找到最大的数并去掉他的因子,剩下的是x和它的因子。在剩下的数中找最大的,就是答案。

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
 int i,j,k,b,n;
 int a[128];
 cin>>n;
 for(i=0;i<n;i++)
 {
  cin>>a[i];
 }
 if(n==2)
 {
  cout<<a[0]<<" "<<a[1]<<'\n';
 }
 else
 {
  sort(a,a+n);
  cout<<a[n-1]<<" ";
  if(a[n-1]==a[n-2])//最后两个最大的数是一样是,那最后俩个数就是  x,y
  {
   cout<<a[n-2]<<'\n';
  }
  else
  {  
   for(i=0;i<n-1;i++)
   {
    if(a[n-1]%a[i]==0)
    {
     if(a[i]==a[i+1])//可能x是y的因数,不要删重复的数
     {
      i++;
     }
     a[i]=0;
    }
   }
   sort(a,a+n);
   cout<<a[n-2]<<'\n';
  }
 }
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值