首页 > 编程 > Java > 正文

JAVA组合递归算法

2019-11-07 22:56:01
字体:
来源:转载
供稿:网友
public static void main(String[] args) { char[] ch = { 'a', 'b', 'c', 'd' }; boolean[] bool = new boolean[ch.length]; combinat(ch, 2, bool, 0); } // num:取几个元素,bool:标记是否取出,start:开始位置 public static void combinat(char[] ch, int num, boolean[] bool, int start) { if (num == 0) { for (int i = 0; i < start; i++) { if (bool[i] == true) { System.out.PRint(ch[i]); } } System.out.println(); return; } if (start == ch.length) { return; } bool[start] = true; combinat(ch, num - 1, bool, start + 1); bool[start] = false; combinat(ch, num, bool, start + 1); }

输出:

abacadbcbdcd
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表