输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻
2008-06-17 19:42
429 查看
原文地址:http://www.java2000.net/viewthread.jsp?tid=6323
先看代码吧:
输出结果
网友提供的一个比较优化的算法
先看代码吧:
import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TreeSet; /** * * @author 赵学庆 www.java2000.net * */ public class T { public static List<String> find(List<String> list) { List<String> rtn = new ArrayList<String>(); String str; for (int i = 0; i < list.size(); i++) { str = list.get(i); list.remove(i); if (list.size() == 0) { rtn.add(str); } else { List<String> sList = find(list); for (String s : sList) { rtn.add(str + s); if (s.length() == 5) { addNumber(str + s); } } } list.add(i, str); } return rtn; } public static void addNumber(String str) { if (str.charAt(2) == '4' || str.contains("35") || str.contains("53")) { return; } set.add(str); } public static Set<String> set = new TreeSet<String>(); public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("2"); list.add("3"); list.add("4"); list.add("5"); find(list); System.out.println(set.size()); int cols = 10; for (String s : set) { System.out.print(s+" "); if(cols--==1) { System.out.println(); cols = 10; } } } }
输出结果
198 122345 122543 123245 123254 123425 123452 125234 125243 125423 125432 132245 132254 132425 132452 132524 132542 142325 142523 143225 143252 145223 145232 152234 152243 152324 152342 152423 152432 212345 212543 213245 213254 213425 213452 215234 215243 215423 215432 221345 221543 223145 223154 223415 223451 225134 225143 225413 225431 231245 231254 231425 231452 231524 231542 232145 232154 232415 232451 232514 232541 241325 241523 242315 242513 243125 243152 243215 243251 245123 245132 245213 245231 251234 251243 251324 251342 251423 251432 252134 252143 252314 252341 252413 252431 312245 312254 312425 312452 312524 312542 315224 315242 315422 321245 321254 321425 321452 321524 321542 322145 322154 322415 322451 322514 322541 325124 325142 325214 325241 325412 325421 341225 341252 341522 342125 342152 342215 342251 342512 342521 345122 345212 345221 412325 412523 413225 413252 415223 415232 421325 421523 422315 422513 423125 423152 423215 423251 425123 425132 425213 425231 431225 431252 431522 432125 432152 432215 432251 432512 432521 451223 451232 451322 452123 452132 452213 452231 452312 452321 512234 512243 512324 512342 512423 512432 513224 513242 513422 521234 521243 521324 521342 521423 521432 522134 522143 522314 522341 522413 522431 523124 523142 523214 523241 523412 523421 541223 541232 541322 542123 542132 542213 542231 542312 542321 543122 543212 543221
网友提供的一个比较优化的算法
import java.util.Set; import java.util.TreeSet /** * 原始版本没有考虑重复问题,我进行了修正.<br> * 此算法的精华就是,让一个字符串的所有位置都进行互换,<br> * 这样就产生了所有可能出现的字符串。 * * @author 赵学庆,nicky_zs www.java2000.net */ public class MyTest { public static void main(String args[]) { char[] number = new char[] { '1', '2', '2', '3', '4', '5' }; perm(number, 0, number.length - 1); System.out.println(set.size()); int cols = 10; for (String s : set) { System.out.print(s + " "); if (cols-- == 1) { System.out.println(); cols = 10; } } } public static void addNumber(String str) { set.add(str); } public static Set<String> set = new TreeSet<String>(); public static void perm(char[] n, int beg, int end) { if (beg == end) { String result = String.valueOf(n); if (n[2] == '4') return; if (result.contains("35") || result.contains("53")) return; addNumber(String.valueOf(n)); return; } for (int i = beg; i <= end; ++i) { swap(n, beg, i); perm(n, beg + 1, end); swap(n, beg, i); } } public static int number = 0; public static void swap(char[] n, int a, int b) { char temp = n[a]; n[a] = n[b]; n[b] = temp; } }
相关文章推荐
- 输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻
- 输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻
- 输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻
- 输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻
- 面试题:输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻
- 输出1,2,2,3,4,5的所有排列组合,4不能在第3位,3和5不能相邻
- 输出{1,2,2,3,4,5}排列组合,4不能在第三位,3和5不能相邻
- 有1,2,2,3,4,5六个数,要求输出他们组合的全排列,4不能在第三位,5和3不能相邻
- 编程:用java 语言实现,输入一个数,就相应地输出的几维数组!||用1、2、2、3、4、5这六个数字,用java写一个函数,打印出所有不同的排列,要求:"4"不能在第三位,"3"与"5"不能相连
- (原创)java 输出排列组合,’4‘ 不能在第三位,‘3’,‘5’不能相连。
- 编程题:输出字符集合的所有排列组合。
- 输出所有排列组合
- 用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
- 输出n对括号的所有有效排列组合
- 1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列, 如:512234、412345等.要求:"4"不能在第三位,"3"与"5"不能相连.
- 122345这6个数字,输出它所有可能的组合;要求4不能在第3位,3和5不能相连。
- 输出字符串的所有排列组合
- 用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列, 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.
- 用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
- 输出N个数中取M个数的所有组合,排列情况