简体   繁体   English

如何从 n 位数字中获得 n/2 位数字组合

[英]How to get n/2-digit combinations from n digit number

I'm struggling with this algorithm.我正在努力解决这个算法。 It should work like this:它应该像这样工作:

If I input fe 6880, my program should output 80 86 80 86 60 68 68.如果我输入 fe 6880,我的程序应该输出 80 86 80 86 60 68 68。

As you can see, combinations are repeating.如您所见,组合不断重复。 That's because it looks at every digit as it is a different object.那是因为它查看每个数字,因为它是一个不同的对象。 In my program it's correct.在我的程序中它是正确的。

Here is my code:这是我的代码:

public static Set<Integer> get2DCombinations(List<Integer> digits) {
    Set<Integer> combinations = new TreeSet<>();

    int t = 0;
    for(Integer i : digits) {
        for (Integer j : digits) {
            t = i * 10 + j;
            if (t / 10 >= 1) {
                combinations.add(t);
            }
        }
    }

    return combinations;
}

It returns a specific set of combinations where all combinations have 2 digits.它返回一组特定的组合,其中所有组合都有 2 位数字。

It works perfectly, but only with 4-digit numbers.它工作得很好,但只能使用 4 位数字。 Of course, I can use one more for-each loops, but is there a way to automate it?当然,我可以再使用一个 for-each 循环,但是有没有办法让它自动化?

So if I input 6-digit number it should output all possible 3-digit combinations of its digits, and if I input 8-digit number, it should output all possible 4-digit combinations.所以如果我输入 6 位数字,它应该输出所有可能的 3 位数字组合,如果我输入 8 位数字,它应该输出所有可能的 4 位数字组合。 Input numbers always have even amount of digits.输入数字总是偶数位数。

Could you please point out for me how to do so?你能帮我指出怎么做吗?

You need a recursive program that will generate all the combinations for your input.您需要一个递归程序来为您的输入生成所有组合。 Here's a solution of mine.这是我的一个解决方案。 My method accepts a String as input (it's just shorted program and easier, you can adapt it to your needs):我的方法接受一个String作为输入(它只是一个简短的程序,更简单,您可以根据自己的需要进行调整):

public static Set<String> get2DCombinations(String input) {
    return backtracking("", input, input.length() / 2) ;
}

public static Set<String> backtracking(String actual, String remaining, int length) {
    if (actual.length() == length) {
        return new HashSet<>(Arrays.asList(actual));
    }

    Set<String> result = new HashSet<>();
    for(int i = 0; i < remaining.length(); i++) {
        result.addAll(backtracking(actual + remaining.charAt(i), remaining.substring(0, i) + remaining.substring(i + 1), length));
    }
    return result;
}

And you call the method like so:你像这样调用方法:

System.out.println(get2DCombinations(input));

Result:结果:

[88, 68, 06, 80, 08, 60, 86]

As I mentioned in the comment, your are missing some of the combinations.正如我在评论中提到的,您缺少一些组合。 This solution generates all of them.此解决方案生成所有这些。

Try calculating n / 2 first.首先尝试计算 n / 2。 So, if n is 6, then n / 2 = 3. Then you know before you start fining the combinations that you are looking for combinations of 3 digits.因此,如果 n 为 6,则 n / 2 = 3。那么您在开始对组合进行优化之前就知道您正在寻找 3 位数字的组合。 Then you want to find the right algorithm to find the combinations.然后你想找到合适的算法来找到组合。 Part of problem solving is breaking down problems to smaller problems.解决问题的一部分是将问题分解为更小的问题。 That is what I did here.这就是我在这里所做的。 I can not solve it for you, however, because it is better for you to solve yourself, and second, there details that you dind't provide so it is hard to give the right solution.但是,我无法为您解决,因为您自己解决更好,其次,您没有提供详细信息,因此很难给出正确的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM