import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class FullSort { //将NUM设置为待排列数组的长度即实现全排列 private static int NUM = 3; private static int count = 0;
/** * 递归算法:将数据分为两部分,递归将数据从左侧移右侧实现全排列 * * @param datas * @param target */ private static void sort(List datas, List target) { if (target.size() == NUM) { for (Object obj : target) System.out.print(obj); System.out.println(); return; } for (int i = 0; i < datas.size(); i++) { List newDatas = new ArrayList(datas); List newTarget = new ArrayList(target);
package CSDN;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class FullSort {
//将NUM设置为待排列数组的长度即实现全排列
private static int NUM = 3;
private static int count = 0;
/**
* 递归算法:将数据分为两部分,递归将数据从左侧移右侧实现全排列
*
* @param datas
* @param target
*/
private static void sort(List datas, List target) {
if (target.size() == NUM) {
for (Object obj : target)
System.out.print(obj);
System.out.println();
return;
}
for (int i = 0; i < datas.size(); i++) {
List newDatas = new ArrayList(datas);
List newTarget = new ArrayList(target);
newTarget.add(newDatas.get(i));
newDatas.remove(i);
sort(newDatas, newTarget);
}
count++;
}
public static void main(String[] args) {
String[] datas = new String[] { "a", "b", "c", "d" };
sort(Arrays.asList(datas), new ArrayList());
System.out.println("一共有" + count + "种排序次数");
}
}楼主参照一下 这个算法
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;public class Test34 { public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Integer num = s.nextInt();
String str = num.toString();
Set set = new HashSet();
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(i !=j){
for(int k=0;k<5;k++){
if(j != k && i != k){
int n = Integer.parseInt(str.charAt(i) + "" + str.charAt(j) + "" + str.charAt(k));
set.add(n);
}
}
}
}
}
Iterator i = set.iterator();
while(i.hasNext()){
System.out.println(i.next());
}
}}