code的变化与重复(组合学)?组合、化与、code

2023-09-10 23:58:49 作者:鱼那么爱水,水却要煮鱼

有没有人有Java的$ C $下产生的所有变化与重演吗?

Does anyone have Java code for generating all VARIATIONS WITH REPETITION?

有足够的可用排列组合的例子,和变化一定是一个最简单的... 感觉愚蠢的浪费时间去重新发明轮子(必须是大量的这种书面code)。

There are plenty of permutation and combination examples available, and variations must be the easiest one... It feels stupid to waste time to reinvent the wheel (it must be plenty of code written for this).

使用REPETITION变异的一个例子可能是这样的:

An example of VARIATIONS WITH REPETITION could be like this:

(tupletSize=3, input= A, B)
AAA, AAB, ABA, BAA, ABB, BAB, BBA, BBB

谢谢!

推荐答案

这可以作为是,它是最容易让你学习。

This works as is, and it's the easiest for you to study.

public class Main {
    public static void main(String args[]) {
        brute("AB", 3, new StringBuffer());
    }
    static void brute(String input, int depth, StringBuffer output) {
        if (depth == 0) {
            System.out.println(output);
        } else {
            for (int i = 0; i < input.length(); i++) {
                output.append(input.charAt(i));
                brute(input, depth - 1, output);
                output.deleteCharAt(output.length() - 1);
            }
        }
    }
}