日期:2014-05-20  浏览次数:20661 次

帮帮忙,排列出可能的组合
有几个数 比如:123
我要把他排列出 可能的组合
比如:132 213 231.....
要怎么 写程序啊

------解决方案--------------------
到底有几个数呢?
是不能确定的还是确定的?
------解决方案--------------------
递归。
写过好几次了,不写了
自己搜一下吧。
------解决方案--------------------
我想的是有多少个数字就循环多少次呢,
------解决方案--------------------
可以用循环写,但要知道是几个数。你这是三个数,则
for (i=1,i<=3,i++){
for (j=1,j<=3,j++){
for (n=1,n<=3,n++){
print i&j&n\t;
}
}
}
------解决方案--------------------
3楼的说用递归,我对编程并不熟,不会写呀。等待赐教。。。
------解决方案--------------------
差不多是这个意思吧:
Java code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javatest;

/**
 *
 * @author ZangXT
 */
public class Main {

    public static void main(String[] args) {
        int a[] = {1, 2, 3,4};
        perm(a, 0, a.length - 1);
    }

    public static void perm(int[] buf, int start, int end) {
        if (start == end) {
            for (int i = 0; i <= end; i++) {
                System.out.print(buf[i] + " ");
            }
            System.out.println();
        } else {
            for (int i = start; i <= end; i++) {
                swap(buf, start, i);
                perm(buf, start + 1, end);
                swap(buf, start, i);
            }
        }
    }

    private static void swap(int buf[], int i, int j) {
        int temp = buf[i];
        buf[i] = buf[j];
        buf[j] = temp;
    }
}

------解决方案--------------------
好麻烦,数值少还好,一旦数值很大时就会占用很大
------解决方案--------------------
探讨
好麻烦,数值少还好,一旦数值很大时就会占用很大

------解决方案--------------------
探讨
差不多是这个意思吧:

Java code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javatest;

/**
*
* @author ZangXT
*/
public class Main {

public static void main(String[] args) {
int a[] = {1, 2, 3,4};
perm(a, 0, a.length - 1);
}

public static void perm(int[] buf, int start, int end) {…

------解决方案--------------------
既然是数字组合,最多10个数字而已!!

0~~9

组合的可能数就是10!(此时n=10,n的阶乘)次!!