【java算法】求5个1~10的数字的所有排列组合,不能重复。

如题:
位置可自由挑换,但是不能重复如:12345,12346,12347,12348,12349这样。

java算法,提供思路也行。

http://blog.csdn.net/mm_bit/article/details/46864403?locationNum=2&fps=1
这个博客里面有很多实现的

www.baidu.com
关键字“全排列 java”

package ceshi;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;

public class CeShiTwo {

public static void main(String[] args) {
    // TODO Auto-generated method stub
     int[] reult1 = randomCommon(1,10,5);  
        for (int i : reult1) {  
            System.out.println(i);  
        }  

}
public static int[] randomCommon(int min, int max, int n){  
        if (n > (max - min + 1) || max < min) {  
               return null;  
           }  
        int[] result = new int[n];  
        int count = 0;  
        while(count < n) {  
            int num = (int) (Math.random() * (max - min)) + min;  
            boolean flag = true;  
            for (int j = 0; j < n; j++) {  
                if(num == result[j]){  
                    flag = false;  
                    break;  
                }  
            }  
            if(flag){  
                result[count] = num;  
                count++;  
            }  
        }  
        return result;  
    }  
}

public static void main(String[] args){
int a,b,c,d,e;
for(a=1; a<10; a++){
for( b=0 ; b<10; b++){
if(b==a) continue;
for(c=0; c<10; c++){
if(c==b || c==a) continue;
for(d=0; d<10; d++){
if(d==c || d==a || d==b) continue;
for(e=0; e<10; e++){
if(e==d || e==a || e==b || e==c) continue;
String str = "";
str = str + Integer.toString(a)+Integer.toString(b)+Integer.toString(c)+Integer.toString(d)+Integer.toString(e);
System.out.println(Integer.parseInt(str));
}
}
}
}
}
}

刚才针对于你的问题,结合深度优先搜索的方法逐步进行了分析,同时编写了源代码,不过正在审核,不确定现在是否能看,这之后我先把完整的代码贴出来,详细解释请看我的博客
http://blog.csdn.net/qq_31382031/article/details/56495705

1 首先输入n(表示有n个数的全排列),然后输入每个数;
2 输出为输入的数的全排列

import java.util.Scanner;
public class BB0103array {

private static int n;
private static int[] a;
private static int[] b;
private static int[] book;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner reader = new Scanner(System.in);

    //读入数据
    //第一个输入的为数字总个数
    n = reader.nextInt();
    a = new int[n+1];//所有元素,相当于牌数
    b = new int[n+1];//放元素的位置,相当于牌要放的盒子
    book = new int[n+1];//标记牌是否已放入盒子
    for(int i=1;i<=n;i++){
        a[i] = reader.nextInt();
    }
    dfs(1);
}

public static void dfs(int step){//step表示现在站在第几个盒子面前
    //边界条件-如果满足到达最后一个位置之后,则输出每个盒子里面的数
    if(step == n+1){
        for(int i=1;i<=n;i++){
            System.out.print(b[i]);
        }
        System.out.println();
        return;
    }

    //当前该做的事情,此时站在第step个盒子面前,该放哪张牌呢?
    for(int j=1;j<=n;j++){
        //判断扑克牌是否还在手上
        if(book[j]==0){
            b[step]=a[j];
            book[j]=1;
            dfs(step+1);
            book[j]=0;
        }   
    }
    return; 
}

}

题目理解有误,在这个之前应加上从1-10中选出五个数,要求这五个数至少有一个是不同的(也就是C^5_10组合的实现)

https://www.zhihu.com/question/56163740

先排列4个数相同的,在排列3个数相同的,接着2个数相同的,最后都不相同的