java 实现 排列 组合

import java.util.ArrayList;

/**
 *
 *
 * permutation and combination
 *
 * @author Administrator
 */
public class Pac {

    Pac(String str) {
        this.str = str;
    }
    String str;//源字符串。  
    String state;//c or a 状态
    /*
     * 组合数 
     */ 
   
    public void permutation(int c) {
        state="a";
        alc = new ArrayList<String>();
        ala = new ArrayList<String>();
        c(str, c);
        for (String s : alc) {
            a(s, c);
        }System.out.println("共计" + ala.size() + "个 组合");
        for (String s : ala) {
            System.out.println(s);
        }
        System.out.println("共计" + ala.size() + "个 组合");
    }
    ArrayList<String> ala = new ArrayList<String>();//返回用的al

    void a(String str, int c) {
        filla(str, c, "");
    }

    void filla(String str, int c, String pre) {
        if (c <= 0) {
            System.out.println("选择的个数太少必须大于0当前是" + c);
            return;
        }
        if (c == 1) {
            ala.add(pre + str.charAt(0));
            return;
        }
        for (int i = 0; i  < str.length(); i++) {
            filla(str.substring(0, i) + str.substring(i + 1, str.length()), c - 1, pre + str.charAt(i));//正常
        }

    }

    /*
     * 排列数
     */
    public void combination(int c) {
        state="c";
        alc = new ArrayList<String>();
        c(str, c);
        for (String s : alc) {
            System.out.println(s);
        }
        System.out.println("共计" + alc.size() + "个 排列");
    }
    /*
     * 内部组合
     */

    private ArrayList<String> c(String str, int c) {
        if (c > str.length()) {
            System.out.println("数组内部出错选择大于数组长度");
            return null;
        }
        fillc(str, c, "");
        return null;
    }
    ArrayList<String> alc = new ArrayList<String>();//返回用的al

    void fillc(String str, int c, String pre) {
        if (c <= 0) {
            System.out.println("选择的个数太少必须大于0当前是" + c);
            return;
        }
        if (c == 1) {
            for (char s : str.toCharArray()) {
                alc.add(pre + s);
            }
            return;
        }
        fillc(str.substring(1, str.length()), c - 1, pre + str.charAt(0));//正常
        //重填
        if (c <= str.length() - 1) {
            fillc(str.substring(1, str.length()), c, pre);
        }
    }

    public static void main(String[] args) {
        Pac p = new Pac("xyz");
        //p.combination(3);
        p.permutation(3);

    }
}
。。。。。


vzw
wvz
wzv
zvw
zwv
vxy
vyx
xvy
xyv
yvx
yxv
vxz
vzx
xvz
xzv
zvx
zxv
vyz
vzy
yvz
yzv
zvy
zyv
wxy
wyx
xwy
xyw
ywx
yxw
wxz
wzx
xwz
xzw
zwx
zxw
wyz
wzy
ywz
yzw
zwy
zyw
xyz
xzy
yxz
yzx
zxy
zyx
共计15600个 组合
成功构建 (总时间: 5 秒) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值