一个排列问题

xx198684 2012-11-17 11:58:51
从1~12中选出6个数字排列,如2 3 8 5 12 6,用java怎样实现列举出所有的排列?有朋友说用hash map,但是有没有更便捷更灵活的方法?比如说可以任选数字的个数,求指教
...全文
118 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xx198684 2012-11-19
  • 打赏
  • 举报
回复
多谢LS两位哈!
raistlic 2012-11-18
  • 打赏
  • 举报
回复
这里是我之前写的排列组合工具类: http://blog.csdn.net/raistlic/article/details/7844812 前面有用法的代码示例,后面有源代码。
qingqingcao22009 2012-11-18
  • 打赏
  • 举报
回复
package exec.basic; public class Paixu { public static void main(String[] args) throws Exception { char[] para = {1,2,3,4,5,6,7,8,9}; for(int i=0;i<para.length;i++) para[i]+=48; paixu(para,para.length,0); } private static void paixu(char[] array, int n, int k) { if (n == k) { char[] out = new char[n]; for (int i = 0; i < array.length; i++) { out[i] = array[i]; } System.out.println(new String(out)); } else { for (int i = k; i < n; i++) { swap(array, k, i); paixu(array, n, k + 1); swap(array, i, k); } } } private static void swap(char[] a, int x, int y) { char temp = a[x]; a[x] = a[y]; a[y] = temp; } }

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧