有1,2,3,4,5,6六个数,要求排列出所有组合情况,但是不能有重复的情况
如:123654,213546等。哪位能帮我解决一下?
------解决方案--------------------
原理很简单:
public class Test{
int[] a1={1,2,3,4,5,6};
int[] a2={1,2,3,4,5,6};
int[] a3={1,2,3,4,5,6};
int[] a4={1,2,3,4,5,6};
int[] a5={1,2,3,4,5,6};
int[] a6={1,2,3,4,5,6};
String s=null;
public static void main(String[] args){
Test test=new Test();
test.paixu();
}
public void paixu(){
for(int i1=0;i1 <6;i1++){
for(int i2=0;i2 <6;i2++){
if(i2==i1) continue;
for(int i3=0;i3 <6;i3++){
if(i3==i1||i3==i2) continue;
for(int i4=0;i4 <6;i4++){
if(i4==i1||i4==i2||i4==i3) continue;
for(int i5=0;i5 <6;i5++){
if(i5==i1||i5==i2||i5==i3||i5==i4) continue;
for(int i6=0;i6 <6;i6++){
if(i6==i1||i6==i2||i6==i3||i6==i4||i6==i5) continue;
s= " "+a1[i1]+a2[i2]+a3[i3]+a4[i4]+a5[i5]+a6[i6];
System.out.println(s);
}
}
}
}
}
}
}
}
------解决方案--------------------
hi! 这么简单, 竟然被他抢先, 哭死!~~
------解决方案--------------------
只写三行吧。六行也是同理。
public class paixu
{
public static void main(String[] args)
{
int i, j, k;
for(i = 1; i <= 3; i++)
for(j = 1; j <= 3; j++)
{
if(i == j) continue;
for(k = 1; k <= 3; k++)
{
if( k == i || k == j) continue;
System.out.println("" + i + j + k);
}
}
}
}
------解决方案--------------------
晕 世界上有个名词叫算法
------解决方案--------------------
嘿嘿。。。。
------解决方案--------------------
老题目了
public class CombineTest {
public static void main() {
int[] a = {1, 2, 3, 4, 5, 6};
int[][] rst;
for (int i=0; i<a.length; i++) {
rst = combine(a, i+1,false);
print(rst);
}
}
protected print(int[][] a) {
System.out.println("-----element " + a[0].length + "-----");
int j;
for (int i=0; i<a.length; i++) {
for (j=0; j<a[i].length-1; j++) {
System.out.print(a[i][j] + ", ");
}
System.out.println(a[i][j]);
}
}
public static int[][] combine(int[] src, int n, boolean sort) {
if (src.length < n) {
return new int[0][0];
}
if (sort) {
Arrays.sort(src);
}
if (n == 1) {
int[][] result = new int[src.length][1];
for (int i=0; i<src.length; i++) {
result[i][0] = src[i];
}
return result;
}
if (src.length == n) {
int[][] result = new int[1][src.length];
for (int i=0; i<src.length; i++) {
result[0][i] = src[i];
}
return result;
}
Vector v = new Vector();
for (int i=0; i<src.length-1; i++) {
int[] sub = new int[src.length-i-1];