交换大法 全排列

来源:互联网 发布:ubuntu分区工具 编辑:程序博客网 时间:2024/06/06 09:37

//能排列重复的元素






public class Change {


/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
int a[]={1,1,2};
ch(a,0);
}
public static void ch(int a[],int start){
if(start==3){
for(int i=0;i<3;i++){
System.out.print(a[i]+" ");
}
System.out.println();
return;
}
for(int i=start;i<3;i++){
if(ok(a,start,i))
{
int temp=a[start];
a[start]=a[i];
a[i]=temp;
ch(a,start+1);
int temps=a[start];
a[start]=a[i];
a[i]=temps;
}
}
}
public static boolean ok(int b[],int start,int end){
for(int i=start;i<end;i++){
if(b[end]==b[i]){
return false;
}
}
return true;
}
}

原创粉丝点击