请问:列出四个数的所有排列组合?

一天十小时 2008-12-04 08:07:14
请问:列出四个数的所有排列组合?
如:
int a[]={2,3,6,7}
现在要得到2,3,6,7所有的排列组合(不能有重复的元素)
2,3,6,7
2,6,7,3
2,7,3,6
3,2,6,7
....
...全文
3396 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
一天十小时 2008-12-16
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 a1b2c3zhang 的回复:]
中学数学就已经学过排列组合的问题了,4*3*2=24种,有这点提示,你应该知道怎么解决了吧!
[/Quote]

等于没说...
pepsighost 2008-12-05
  • 打赏
  • 举报
回复


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();
}

}
}

}

}
}
}


wangming402407250 2008-12-05
  • 打赏
  • 举报
回复
用八皇后的解决思路来做
daniel_kaka 2008-12-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 chenchendf 的回复:]
引用 2 楼 daniel_kaka 的回复:
关于排列组合,这里比较详细:
http://danielcao2008.blog.163.com/blog/static/820385012008101111182788/


晕,"去掉一个已经排好序的数组的重复数字,速度尽量快",这是排列组合?
[/Quote]

实在抱歉:地址copy错了,看这个“排列组合子集合的算法”
http://danielcao2008.blog.163.com/blog/static/820385012008510595347/
cotton5i 2008-12-05
  • 打赏
  • 举报
回复
我在考虑递归算法,最小可分到2个元素的时候
可是如果是3个元素,怎么让它往下分解呢?
嗯,关注一下
一天十小时 2008-12-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 daniel_kaka 的回复:]
关于排列组合,这里比较详细:
http://danielcao2008.blog.163.com/blog/static/820385012008101111182788/
[/Quote]

晕,"去掉一个已经排好序的数组的重复数字,速度尽量快",这是排列组合?
一天十小时 2008-12-05
  • 打赏
  • 举报
回复
大家看看这个行不行:
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

总觉得用这么多层的循环不是很好,有没有其它的方法?是否可以运算递归??????
daniel_kaka 2008-12-05
  • 打赏
  • 举报
回复
关于排列组合,这里比较详细:
http://danielcao2008.blog.163.com/blog/static/820385012008101111182788/
一天十小时 2008-12-05
  • 打赏
  • 举报
回复
难道要用四层for循环???
为什么没有人回答啊

自己顶
vincentjang 2008-12-05
  • 打赏
  • 举报
回复

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;
}
}


怎么不可以?
vincentjang 2008-12-05
  • 打赏
  • 举报
回复
递归方法可以的
有本数据结构的书上有介绍
可惜我看不太懂……
一天十小时 2008-12-05
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 vincentjang 的回复:]
用递归看起来比较简单
9楼的加个swap()方法看起来更清楚

Java code
public static void swap(int x,int y){
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
[/Quote]
你这个方法达不到交换的效果.
a1b2c3zhang 2008-12-05
  • 打赏
  • 举报
回复
八楼是正解。

另外,这里根本不涉及什么递归!
a1b2c3zhang 2008-12-05
  • 打赏
  • 举报
回复
中学数学就已经学过排列组合的问题了,4*3*2=24种,有这点提示,你应该知道怎么解决了吧!
pepsighost 2008-12-05
  • 打赏
  • 举报
回复

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--;

}

}



vincentjang 2008-12-05
  • 打赏
  • 举报
回复
用递归看起来比较简单
9楼的加个swap()方法看起来更清楚

public static void swap(int x,int y){
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
一天十小时 2008-12-05
  • 打赏
  • 举报
回复
还有:
上面的递归我不是很明白,有点迷糊,大家可不可以简单分析讲一下,谢谢
一天十小时 2008-12-05
  • 打赏
  • 举报
回复

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;写到一个方法内???

62,636

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧