B. 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 d is a divisor of both numbers x and y at the same time, there are two occurrences of d in the list.
For example, if x=4 and 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,…,dn (1≤di≤104), where di is either divisor of x or divisor of y. If a number is divisor of both numbers x and y then there are two copies of this number in the list.
Output
Print two positive integer numbers x 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。(这个就是这题思维跳跃的一个点)
然后,一个for循环,判断x取余数组的每个数,如果x取余后等于零而且没有被标记过,则标记为1;剩下的存入另一个数组里,找出数组中没有标记的最大的数也一定是另一个整数;

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int a[150];
int b[150];
int vis[10005];
int main()
{
	int n;
	cin>>n;
	for(int t=0;t<n;t++)
	{
		scanf("%d",&a[t]);
	}
	sort(a,a+n);
	int l=a[n-1];
	int s=0;
	for(int t=0;t<n;t++)
	{
		if(l%a[t]==0&&vis[a[t]]==0)//用另一个数组标记没有出现过的被除数;
		{
			vis[a[t]]=1;//标记出现过了
			continue;
		}
		else
		{
			b[s++]=a[t];//存入另一个数组;
		}
	}
	sort(b,b+s);//再排序,找另一个的最大值,这个最大值就是另一个整数
	cout<<l<<" "<<b[s-1]<<endl;
	return 0;
}

最后附上翻译:

每次测试的时间限制为1秒
每次测试的内存限制256兆字节
输入标准输入
输出标准输出
最近你收到了两个正整数x和y。你忘记了它们,但你记得一个包含x(包括1和x)的所有除数和y的所有除数(包括1和y)的混洗列表。如果d是数字x和y同时的除数,则列表中出现两次d。
例如,如果x = 4且y = 6,则给定列表可以是列表[1,2,4,1,2,3,6]的任何排列。一些可能的清单是:[1,1,2,4,6,3,2],[4,6,1,1,2,3,2]或[1,6,3,2,4, 1,2]。
你的问题是恢复合适的正整数x和y,它会产生相同的除数列表(可能是不同的顺序)。
保证答案存在,即给定的除数列表对应于一些正整数x和y。
输入
第一行包含一个整数n(2≤n≤128) - x和y的除数。
输入的第二行包含n个整数d1,d2,…,dn(1≤di≤104),其中di是x的除数或y的除数。如果数字是数字x和y的除数,那么列表中有这个数字的两个副本。
产量
打印两个正整数x和y - 这些合并其除数列表的数字是给定整数列表的排列。保证答案存在。
Example
input
10
10 2 8 1 2 4 1 20 4 5
output
20 8

猜你喜欢

转载自blog.csdn.net/qq_43516113/article/details/89028137