英文:
Find minimum number of integers >=2 that divide all array elements
问题
我必须找到最小数量的整数(其中每个整数>=2),它们能够整除数组中的所有元素。
例如,如果数组是 [2,4,6,8,10]
,那么答案是 1(因为 2 可以整除数组中的所有元素)。
另一个例子,如果数组是 [2,3,4,9]
,那么答案是 2(因为我们至少需要 2 个不同的数字,2 和 3,满足条件)。
我决定生成给定数组的所有子数组,存储在一个二维数组中,然后按照它们大小的降序对所有子数组进行排序。然后遍历这个二维数组,一旦找到一个可以整除子数组所有元素的数字,就返回该子数组的索引+1。
所以,对于上面的第一个例子,首先要检查的子数组将是数组本身(在排序后),并且会找到一个数字(2)可以整除它的所有元素。因此,返回值将是 index+1=0+1=1
。然而,这并不总是适用。编程语言是 Java。
英文:
I have to find minimum number of integers, (where each such integer>=2) that divide all array elements.
For example, if the array is [2,4,6,8,10]
then the answer is 1 (because 2 divides all array elements).
Another example, if the array is [2,3,4,9]
then the answer is 2 (because we need at least 2 different numbers, 2 and 3, that satisfy the conditions).
I decided to generate all sub-arrays of the given array, store in a 2D array, then sort all sub-arrays in descending order of their sizes. Then iterate through the 2D array, and as soon as a number that divides all elements of a sub-array is found, return the index of that sub-array+1.
So, for the first example above, the first sub-array to be checked would be the main array itself (after sorting), and a number (2) will be found that divides all its elements. So, the return value would be index+1=0+1=1
. However, this does not always work. Language is Java.
答案1
得分: 1
对于每个数字来说,质因数是决定性的:12 = 2 * 2 * 3,因此需要知道{2,3}。
有了这样一个包含多个集合(质因数集合)的列表后,你需要找出一组数字,使得它们可以组合成所有的集合。
你需要在纸上设计出这样一个算法,说明你会如何去做。然后将这个算法编写成代码。
例如,对于只有一个因数 49 的集合:{7},这意味着 7 必然 是一个分类数字。
对于在模棱两可的选择之间寻找最小值,比如说 {2,3}、{3,5}、{5,7},你需要考虑一些方法。
祝你好运。
英文:
For every number the prime factors are decisive: 12 = 2 * 2 * 3, so {2, 3} is needed knowledge.
Having such a list of sets, prime factors, you need to find a set of numbers that then make up all sets.
Such an algorithm you work out on paper, how you would do it. And then code your algorithm out.
For instance a set with only one factor 49: {7} would imply, that 7 must be a categorizing number.
For a minimum between ambiguous choices, say {2, 3}, {3, 5}, {5, 7} you have to think of something.
Good luck.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论