我无法正确计算数字

时间:2017-09-11 19:00:22

标签: java arrays arraylist

帮助,我制作了一个允许计算数字排列的代码。例如,如果我输入数字2090,则返回解决方案:

2090,0092,0290,0209,9200,9002,9020,0029,0920,0902,2900,2009,2090,0092,0290,0209,9200,9002,9020,0029,0920,0902,2900,2009。

然后过滤解决方案,使它们是11的倍数,没有零,不重复。如果我使用相同的数字(2090)以下解决方案,这个过滤器给了我:

2090 9020

这里是我遇到问题的地方,如果我输入例如数字2900应该给我与进入2090时相同的解决方案,但我只返回一个9020的解决方案 我该如何解决这个错误?

这是代码:

public static void main(String[] args) {

        ArrayList<String> list = new ArrayList<String>();
        Scanner read = new Scanner(System.in);

        System.out.println("Enter a Number");
        String n = read.next();

        //Calculate number of permutations
        long nPer = 1;
        for (int i = 1; i <= n.length(); i++) {
            nPer *= i;

        }
        System.out.println("Number of Permutations:" + nPer);
        char nums[] = n.toCharArray();

        //performs the permutation of the number according to the number of permutations
        for (int i = 0; i < nPer; i++) {

            /*
            valid if it is multiple of 11, does not have 0 to the left or if it is repeated and adds it to the ArrayList called list
             */

            if (Long.parseLong(String.valueOf(nums)) % 11 == 0) {
                if (nums[0] != '0') {
                    if (!list.contains(String.valueOf(nums))) {
                        list.add(String.valueOf(nums));
                    }
                }
            }

            char t = nums[i % (nums.length - 1)];
            nums[i % (nums.length - 1)] = nums[nums.length - 1];
            nums[nums.length - 1] = t;

        }
        //Display list with final solutions
        System.out.println("______________");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        System.out.println(list.size());

    }

1 个答案:

答案 0 :(得分:0)

这就是发布mcve非常有用的原因:
当您消除过滤器时:

public static void main(String[] args) throws IOException {

    String n = "2900";
    ArrayList<String> list = new ArrayList<>();

    //Calculate number of permutations
    long nPer = 1;

    for (int i = 1; i <= n.length(); i++) {
        nPer *= i;
    }

    char nums[] = n.toCharArray();

    //performs the permutation of the number according to the number of permutations
    for (int i = 0; i < nPer; i++) {

        list.add(String.valueOf(nums));

        char t = nums[i % (nums.length - 1)];
        nums[i % (nums.length - 1)] = nums[nums.length - 1];
        nums[nums.length - 1] = t;
    }
    //Display list with final solutions

    System.out.println("______________");
    for (String s:  list) { System.out.println(s); }       
}

你得到:

  

2900 0902 0209 0290 0290 0092 0029 9020 9020 9002 2009 2900 2900 0902   0209 0290 0290 0092 0029 9020 9020 9002 2009 2900

如您所见,2090组合未生成。另一方面,其他组合出现不止一次。 所以现在很清楚你需要研究:

        char t = nums[i % (nums.length - 1)];
        nums[i % (nums.length - 1)] = nums[nums.length - 1];
        nums[nums.length - 1] = t;

(在添加复杂性之前,始终验证一段代码是否按预期工作)
要正确查找排列,请尝试:

ArrayList<String> list = new ArrayList<>(permutation(n));

private static Set<String>  permutation(String s) {

    if((s == null) || (s.length() < 2) ) {
        return null;
    }

    Set<String> permutations = new HashSet<>();
    permutation(0,s,permutations);
    return permutations;
}

private static void  permutation(int index, String s, Set<String> permutations) {

    permutations.add(s);

    if(index >= (s.length()-1)) {
        return;
    }

    char[] chars = s.toCharArray();

    for(int pos=0; pos < s.length() ; pos++) {

        char[] temp  = s.toCharArray();

        char c = temp[pos];
        temp[pos] = chars[index];
        temp[index] = c;

        permutation(index +1, String.valueOf(temp),  permutations);
    }

}