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

全部评论

相关推荐

面试官全程关摄像头1.自我介绍一下2.React和Vue哪个更熟悉一点3.你在之前那段实习经历中有没有什么技术性的突破(我只是实习了44天工作28天,我把我能说的都说了)4.你封装的哪个表单组件支不支持动态传值5.自己在实习阶段Vue3项目封装过hook吗6.hook有什么作用7.Vue2和Vue3的响应式区别(我说一个是proxy是拦截所有的底层操作,Object.defineProperty本身就是一个底层操作,有些东西拦截不了,比如数组的一些操作还有等等,面试官就说实在要拦截能不能拦截????我心想肯定不行呀,他的底层机制就不允许吧)8.pinia和vuex的区别(这个回答不出来是我太久没用了)9.pinia和zustand的区别,怎么选(直接给我干懵了)(我说react能用pinia吗&nbsp;&nbsp;他说要用的话也可以)10.渲染一万条数据,怎么解决页面卡顿问题(我说分页、监听滚轮动态加载,纯数据展示好像还可以用canvas画)(估计是没说虚拟表单,感觉不满意)11.type和interface的区别12.ts的泛型有哪些作用(我就说了一个结构相同但是类型不同的时候可以用,比如请求响应的接口,每次的data不同,这里能用一个泛型,他问我还有什么)13.你项目用的是React,如果让你再写一遍你会选择什么14.pnpm、npm、yarn的区别15.dependencies和devdependencies的区别总而言之太久没面试了,上一段实习的面试js问了很多。结果这次js一点没问,网络方面也没考,表现得很一般,但是知道自己的问题了&nbsp;&nbsp;好好准备,等待明天的影石360和周四的腾讯了&nbsp;&nbsp;加油!!!
解zj:大三的第一段面试居然是这样的结局
查看15道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务