本帖最后由 zhuoyan825 于 2014-06-06 12:25:05 编辑

解决方案 »

  1.   

    去百度java排列组合吧 一大堆呢
      

  2.   

    public class Test { public static int func(int[] value, int n) {
    int result = 0;
    if (value.length < n) {
    return 0;
    } else {
    for (int i = 0; i <= value.length - n; i++) {
    int temp = 1;
    for (int j = 0; j < n; j++) {
    temp *= value[i + j];
    }
    result += temp;
    }
    }
    return result;
    } public static void main(String[] args) {
    int[] value = { 2, 3, 1, 3 };
    int n = 2;
    System.out.println(func(value, n));
    }
    }我没有考虑溢出问题,如果实际实用,数据量比较大的话,需要考虑溢出问题。
      

  3.   

    public class Test {
    private static int result;

    public static void main(String[] args) {
    int[] set = {2, 3, 1, 3};
    int n = 4;

    for (int k = 2; k <= n; k++) {
    result = 0;
    int[] subset = new int[k];
    calc(set, subset, set.length, k);

    System.out.printf("k = %d, Sn = %d%n", k, result);
    }

    }

    public static void calc(int[] set, int[] subset, int n, int k) {
    if (k == 0) {
    int t = 1;
    for (int i = 0; i < subset.length; i++) {
    t *= subset[i];
    }
    result += t;

    return;
    }

    for (int i = n; i >= k; i--) {
    subset[k - 1] = set[i - 1];
    calc(set, subset, i - 1, k - 1);
    }
    }
    }
    输出
    k = 2, Sn = 29
    k = 3, Sn = 39
    k = 4, Sn = 18
      

  4.   

    经过大半天的努力,我也完成了
    public class test {
    /**
     * 递归求解组合数,并输出组合数序列
     * @param a  待求解组合数原始数组
     * @param n  组合数原始数组大小
     * @param m  所求组合数的个数
     */
    public static int combine(Integer[] a, Integer n, Integer[] b, Integer m, final Integer mFinal) {
    int Result = 0;
    for (int i = n; i >= m; i--) {
    b[m - 1] = i - 1;
    if (m > 1) { // 递归求解
    Result += combine(a, i - 1, b, m - 1, mFinal);
    } else {
    int Sn = 1;
    for (int j = 0; j <= mFinal - 1; j++) {
    Sn = Sn * a[b[j]];
    }
      Result += Sn; 
    }
    }
    return Result;
    } public static void main(String[] args) {
    Integer[] a = new Integer[] { 1, 2, 3, 2, 3, 1, 1, 2};
    Integer n = a.length;
    Integer m =7, mFinal = 7;
    Integer[] b = new Integer[m];
    System.out.println(combine(a, n, b, m, mFinal));
    }}