java - 使用 Java 找出独特的因素

标签 java algorithm arraylist

问题: 打印给定数字的所有唯一因子组合(1 除外)。

例如: 输入:12

输出:[[2, 2, 3], [2, 6], [3, 4]]

我的解决方案:

public class Unique_factor {
    public static void main(String args[]) {
        int number = 12;
        ArrayList<ArrayList<Integer>> combination = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> abc = new ArrayList<>();
        for(int i = 2; i <= number; i++) {
            if(number % i == 0) {
                abc.add(i);
                int result = number;
                for(int j = i; j <= (number/i); j++) {
                    if(result % j == 0) {
                        result = result / j;
                        abc.add(j);
                    }
                }
            }
        }

        //System.out.println(combination);
        System.out.println(abc);
    }
}

输出:

[2, 2, 3, 3, 3, 4, 4, 6, 12]

根据我的代码,它打印出 12 的所有可能因数。j 循环迭代直到 (number/i)。我创建了一个名为 combination 的列表类型 ArrayList 列表来创建列表列表,但我不知道如何使用它。我应该在哪里更改我的代码?

最佳答案

我想出了以下方法来查找数字的唯一因子。不过,它比您之前尝试过的要复杂一些,可能有更好的解决方案,但该方法似乎可以正常工作。

public class UniqueFactors {
    public static void main(String[] args) {
        int input = 12; // Currently, the output is blank if the input is 1
        ArrayList<ArrayList<Integer>> combinations = new ArrayList<>();

        for (int i = 2; i <= input; i++) {
            int result;
            if (input % i == 0) {
                result = input / i;
                ArrayList<Integer> factorSet = new ArrayList<>();
                factorSet.add(i);
                boolean moreFactors = false;
                int result2 = result;
                for (int j = 2; j <= result2; j++) {
                    if (result2 % j == 0) {
                        moreFactors = true;
                        factorSet.add(j);
                        result2 = result2 / j;
                        j = 1; // Reset to one because it will be added to on the next iteration
                    }
                }
                if (!moreFactors) factorSet.add(result);
                //> The following chunk just gets rid of duplicate combinations that were in different orders
                boolean copy = false;
                for (int k = 0; k < combinations.size(); k++) {
                    if (combinations.get(k).size() == factorSet.size()) {
                        Collections.sort(combinations.get(k));
                        Collections.sort(factorSet);
                        if (combinations.get(k).equals(factorSet)) {
                            copy = true;
                            break;
                        }
                    }
                }
                if (!copy) combinations.add(factorSet);
            }
        }

        for (int i = 0; i < combinations.size(); i++) {
            System.out.println(combinations.get(i));
        }
    }
}

输出:

[2, 2, 3]
[3, 4]
[2, 6]
[1, 12]

希望这篇文章能以某种方式提供帮助。

关于java - 使用 Java 找出独特的因素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53963465/

相关文章:

java - GWT SimplePager - 为每个下一个和上一个添加历史记录

java - 在 Java 中获取 NoClassDefFoundError

java - 将字符串数组列表转换为字符数组

forms - 如何通过表单中的索引访问 session ArrayList?

java - 将数组插入到 int 数组的数组列表的正确位置

java - 我怎么知道谁调用了 System.gc()?

c - 数组中的最大值及其频率

algorithm - 删除所有不在任何路径上且 sum>= k 的节点

java - 如何从 {a1|a2|a3} 格式字符串中获取 N 个随机字符串?

android - 如何在 SharedPreferences 中存储 ArrayList<HashMap<String, String>> ?