code的变化与重复(组合学)? [英] Code for Variations with repetition (combinatorics)?

查看:190
本文介绍了code的变化与重复(组合学)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有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);
            }
        }
    }
}

这篇关于code的变化与重复(组合学)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆