public class ChuHe { public static void main(String[] args) { int[] a = { 2, 4, 7, 9 }; int[] b = new int[4]; for (int i = 0; i < a.length; i++) { b[0] = a[i]; for (int j = 0; j < a.length; j++) { b[1] = a[j]; for (int k = 0; k < a.length; k++) { b[2] = a[k]; for (int m = 0; m < a.length; m++) { b[3] = a[m]; if (i != j && i != k && i != m && j != k && j != m & k != m) { for (int p = 0; p < b.length; p++) { System.out.print(b[p]); } System.out.println(); } } } } } } }
public class ArrayCombination { static int[] a = {1, 2, 3}; static int count = 0; public static void main(String[] args) { permutation(0, a.length-1); System.out.println("一共有" + count + "种组合"); }
public static void permutation(int m, int n) { int i; if (m == n) { display(a); count++; } for (i = m; i <= n; i++) { swap(i,m); permutation(m + 1, n); swap(i,m); } } public static void display(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i]); } System.out.println(); } public static void swap(int x,int y){ int temp = a[x]; a[x] = a[y]; a[y] = temp; } } 怎么不可以?
为什么没有人回答啊自己顶
http://danielcao2008.blog.163.com/blog/static/820385012008101111182788/
package org.chen.test;
public class NumberCalcu {
/**
* @param args
*/
public static void main(String[] args) {
int[] a=new int[]{2,6,3,7};
new NumberCalcu().array(a,a.length);
}
// 打印出元素的所有组合
public void array(int a[],int len){
java.util.Arrays.sort(a);
for(int i=0;i<len;i++){
for(int j=0;j<len;j++){
if(j==i) continue;
label:
for(int k=0;k<len;k++){
if(k==j || k==i) continue;
for(int p=0;p<len;p++){
if(p==k || p==j || p==i) continue;
System.out.print(a[i]+" ");
System.out.print(a[j]+" ");
System.out.print(a[k]+" ");
System.out.println(a[p]+" ");
continue label;
}
}
}
}
}
}
运行结果:
2 3 6 7
2 3 7 6
2 6 3 7
2 6 7 3
2 7 3 6
2 7 6 3
3 2 6 7
3 2 7 6
3 6 2 7
3 6 7 2
3 7 2 6
3 7 6 2
6 2 3 7
6 2 7 3
6 3 2 7
6 3 7 2
6 7 2 3
6 7 3 2
7 2 3 6
7 2 6 3
7 3 2 6
7 3 6 2
7 6 2 3
7 6 3 2 总觉得用这么多层的循环不是很好,有没有其它的方法?是否可以运算递归??????
实在抱歉:地址copy错了,看这个“排列组合子集合的算法”
http://danielcao2008.blog.163.com/blog/static/820385012008510595347/
int[] b = new int[4];
for (int i = 0; i < a.length; i++) {
b[0] = a[i];
for (int j = 0; j < a.length; j++) {
b[1] = a[j];
for (int k = 0; k < a.length; k++) {
b[2] = a[k];
for (int m = 0; m < a.length; m++) {
b[3] = a[m]; if (i != j && i != k && i != m && j != k && j != m
& k != m) {
for (int p = 0; p < b.length; p++) {
System.out.print(b[p]);
}
System.out.println();
} }
} } }
}
}
public class ArrayCombination {
static int a[] = {1,2,3,4};
static void permutation(int m,int n)
{
int i;
if(m==n)
{
for(i = 0;i<=n;i++)
{
print(a[i]);
}
print("\n");
}
for(i = m;i<=n;i++)
{
int t=a[i];a[i]=a[m];a[m]=t;
permutation(m+1,n);
t=a[i];a[i]=a[m];a[m]=t;
}
}
public static void print(Object o) {
System.out.print(o.toString());
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
permutation(0,3);
}}
呵呵,查到了,用递归做的,
但是大家看看能不能把t=a[i];a[i]=a[m];a[m]=t;写到一个方法内???
9楼的加个swap()方法看起来更清楚public static void swap(int x,int y){
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
public class Test2 {
char[] d=null;
int p=-1;
public static void main(String[] args) {
new Test2().permute("2367"); }
public void permute(String str) {
d=new char[str.length()];
permute(str.toCharArray(),0, str.length()-1);
}
public void permute(char[] str, int low, int high) {
if(low>high){
System.out.println(new String(d,0,p+1));
return;
}
p++;
for(int i=low;i<=high;i++) {
char a=str[i];
str[i]=str[low];
str[low]=a;
d[p]=str[low];
permute(str,low+1, high);
char b=str[i];
str[i]=str[low];
str[low]=b;
}
p--;
}}
public class ArrayCombination { static int[] a = {1, 2, 3};
static int count = 0; public static void main(String[] args) {
permutation(0, a.length-1);
System.out.println("一共有" + count + "种组合");
}
public static void permutation(int m, int n) {
int i;
if (m == n) {
display(a);
count++;
}
for (i = m; i <= n; i++) {
swap(i,m);
permutation(m + 1, n);
swap(i,m);
}
} public static void display(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
}
System.out.println();
} public static void swap(int x,int y){
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
怎么不可以?