java打印出一组数字的所有排列。

例如:123456,打印出123456的所有排列:123456,156324,523641,156243,263541.....
不允许用到集合类,如:List,Map,Set集合都不能用到,用纯Java算法做出来。简洁易懂。请哪位大侠指教。万分感谢。

下面是个排列组合生成的算法,我电脑上正好有,NetBeans测试通过,你要1到6的数字组合,运行时就输入6。有比较详细的注释,你可以参考一下。

package permutationandcombination;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class PermutationAndCombination
{
//要排列组合的元素个数
private static int MAX_INDEX;
//当前排列中需要填入数字的索引位置
private static int finishIndex;
//已经完成的排列的数量
private static int finishCount;
//记录排列元素的数组
private int[] num;
//当前的排列组合
private LinkedList<Integer> savedNum;

public PermutationAndCombination(int max)
{
MAX_INDEX = max;
finishIndex = 0;
finishCount = 0;
num = new int[MAX_INDEX];
savedNum = new LinkedList<Integer>();
for(int i=0; i<MAX_INDEX; i++)
{
num[i] = i+1;
}
}

public void doPermutationAndCombination()
{
saveNum(num);
System.out.println("一共 " + finishCount + "种组合!");
}
//完成排列组合,并输出到屏幕
private void saveNum(int[] num)
{
//循环数量由所处的递归层数决定
for(int i=0; i<MAX_INDEX-finishIndex; i++)
{
//添加选中的元素到链表
savedNum.addLast(num[i]);
//记录已经选取的元素
int numBuf = num[i];
//记录以完成的排列组合数量
if(finishIndex == (MAX_INDEX-1))
{
finishCount++;
}
//创建传入递归下一层要用的数组
int nextNum[] = new int[MAX_INDEX - (finishIndex+1)];
int m = 0;
//拷贝未选用的数字
for(int n=0; n<MAX_INDEX-finishIndex; n++)
{
if(num[n] == numBuf)
{
continue;
}
else
{
nextNum[m] = num[n];
m++;
}
}
//是否继续递归
if((MAX_INDEX - (finishIndex+1)) != 0)
{
//递归层数计数加1
finishIndex++;
saveNum(nextNum);
}
else
{
//输出这一轮递归生成的数字组合
System.out.println(savedNum);
}
}
try
{
//判断是否是递归的最后一层
if(finishIndex == (MAX_INDEX-1))
{
//移除排列组合的最后一位元素
savedNum.removeLast();
//移除排列组合的最后一位元素
savedNum.removeLast();
}
else
{
//移除排列组合的最后一位元素
savedNum.removeLast();
}
}
catch(Exception e){}
finally
{
//回到上一层,递归层数要减1
finishIndex--;
}
}

public static void main(String[] args)
{
String theMaxString = null;
int theMax = 0;
try
{
System.out.print("请输入数字: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
theMaxString = br.readLine().trim();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
theMax = Integer.parseInt(theMaxString);
}
catch(Exception e)
{
System.out.println("输入的数字格式不正确!");
}
PermutationAndCombination p = new PermutationAndCombination(theMax);
Date date = new Date();
p.doPermutationAndCombination();
System.out.println("用时" + (new Date().getTime() - date.getTime()) + "毫秒");
}
}

楼上的,你怎么知道我没测试?我不测试我是不会贴上来的。
run:
请输入数字: 3
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
一共 6种组合!
用时0毫秒
成功生成(总时间:3 秒)
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-06-03
public class Loop {
public static void main(String[] args) {
// 求组成如下字符串的所有字符可能的组合
String str = "123456";
// 组合后字符串长度,当为元素无重复模式时,这个值要不大于元素个数
int len = 6;
// loop(new char[]{},str.toCharArray(),len);
loop(new char[] {}, str.toCharArray(), len);
}

// 元素无重复
public static void loopNoDup(char[] t, char[] a, int l) {
if (l > 1) {
for (int i = 0; i < a.length; i++) {
char[] b = new char[a.length - i - 1];
System.arraycopy(a, i + 1, b, 0, b.length);
char[] c = new char[t.length + 1];
System.arraycopy(t, 0, c, 0, t.length);
c[t.length] = a[i];
loopNoDup(c, b, l - 1);
}
} else {
for (int i = 0; i < a.length; i++) {
process(t, a[i]);
}
}
}

// 元素可重复
public static void loop(char[] t, char[] a, int l) {
if (l > 1) {
for (int i = 0; i < a.length; i++) {
char[] b = new char[t.length + 1];
System.arraycopy(t, 0, b, 0, t.length);
b[t.length] = a[i];
loop(b, a, l - 1);
}
} else {
for (int i = 0; i < a.length; i++) {
process(t, a[i]);
}
}
}

private static void process(char[] t, char c) {
for (char i : t)
System.out.print(i);
System.out.println(c);
}

public static final void nestedForLoop(int[][] d,int x,int y){
if(y==d[x].length)
return;
ring(d,x);
if(x>0){
nestedForLoop(d,x-1,0);
nestedForLoop(d,x,y+1);
}
else{
print(d);
nestedForLoop(d,x,y+1);
}
}

private static final void ring(int[][] d,int x) {
int t=d[x][d[x].length-1];
for(int i=d[x].length-1; i>0; i--)
d[x][i]=d[x][i-1];
d[x][0]=t;
}

private static final void print(int[][] d) {
for(int i=0; i<d.length; i++)
System.out.print(d[i][0]);
System.out.println();
}
}
第2个回答  2010-06-03
这个比较简单,可以用户输入,只是不能判断重复。
import java.io.*;
class test{
String s;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
test(){
try{s=br.readLine();
}catch(IOException e){System.err.println("输入错误!");}
char[] c=new char[s.length()];
c=s.toCharArray();
System.out.println("排列如下:");
setChar(c,0,s.length()-1);
}
void setChar(char[] cc,int a,int b){
String ss=String.valueOf(cc);
if(a==b)show(cc);
for (int i=a; i<b+1;i++){
char t=cc[a];cc[a]=cc[i];cc[i]=t;
setChar(cc,a+1,b);
cc=ss.toCharArray();
}
}
void show(char[] chars){
System.out.println(String.valueOf(chars));
}
}
public class AllSet{
public static void main (String[] args) {
new test();
}
}
第3个回答  2010-06-03
额,一楼仁兄,测试了再贴上来。
相似回答