输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数, 使其和等于 m ,要求将其中所有的可能组合出来---经典数据结构第21道

import java.util.LinkedList;

/**
 * 中兴面试题(2010) 输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数, 使其和等于 m ,要求将其中所有的可能组合列出来.
 * 动态规划思想 将findCombination(m,n)分解成加入n的findCombination(m - n, n -
 * 1)与不加入n的findCombination(m, n - 1)两个子问题,递归求解 相当于从n-->1 慢慢开始验证
 * 边界条件:m==0,表示正好和为m,可以显示数据并结束;
 * n<=0||m<0都必须直接结束,n<=0表示从右到左验证,并到最左端,再无数据,m<0表示刚减去n,已成负值,不符合要求了.
 * 注意:当m<n时,很显然比m大的数肯定不是组合中的数,故此直接从1,2,3......m中找和为m.
 * 
 * @author lyric
 * 
 */
public class ZTECombination2 {

    private static LinkedList<Integer> list = new LinkedList<Integer>();

    public static void findCombination(int m, int n) {
        // n>m 相当于直接从1...m中找合适组合
        if (n > m) {
            n = m;
        }
        if (m == 0) {
            showDate();
            return;
        }
        if (n <= 0 || m < 0) {
            return;
        }
        list.push(n);// 加入n 递归计算
        findCombination(m - n, n - 1);
        list.pop(); // 回退到 不加入n
        findCombination(m, n - 1);

    }

    /**
     * 打印list数据
     */
    private static void showDate() {
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int m = 8;
        int n = 20;
        findCombination(m, n);
    }
}

/**
 * 使用July的背包策略
 * 从索引0到n-1开始验证
 * 递归结束条件:sum=0,表示找到一个组合,此时要输出数据,并退出;sum<0,和已经超过m了,组合不满足,退出.
 *          index索引为n时,表示已经超过了最大索引,也需要退出.
 * @author lyric
 *
 */
public class ZTECombination {

    /**
     * 
     * @param sum
     *            总和
     * @param index
     *            索引
     * @param aux
     *            辅助数组
     * @param n
     */
    private static void helper(int sum, int index, int[] aux, int n) {
        //m为0时,要输出数据
        if (sum == 0) {
            showData(aux, n);
            return;
        }
        if (sum < 0 || index == n) {
            return;
        }
        helper(sum, index + 1, aux, n);
        //已选择了数字index+1
        aux[index] = 1;
        //m要减去index+1,继续递归
        helper(sum - index - 1, index + 1, aux, n);
        //回退操作,取消选择的数字,继续尝试找新的组合
        aux[index] = 0;
    }

    private static void showData(int[] aux, int n) {
        for (int i = 0; i < n; i++) {
            if (aux[i] == 1) {
                System.out.print(i + 1 + " ");
            }
        }
        System.out.println();
    }

    public static void findCombination(int m, int n) {
        //当m<n时,很显然比m大的数肯定不是组合中的数
        if (n > m) {
            n = m;
        }
        int[] aux = new int[n];
        helper(m, 0, aux, n);
    }

    public static void main(String[] args) {
        int m = 8;
        int n = 20;
        findCombination(m, n);
    }
}

/**
 * 回溯 递归结束条件:一样 
 * 注意:只不过使用下面的方法回避了m与n的大小关系
 * @author lyric
 * 
 */
public class ZTECombination3 {

    private static void findCombination(int sum, int index, int[] aux, int n) {
        // m为0时,要输出数据
        if (sum == 0) {
            showData(aux, n);
            return;
        }
        if (sum < 0 || index == n) {
            return;
        }
        // 选择index索引
        aux[index] = 1;
        // m要减去index+1,递归
        findCombination(sum - index - 1, index + 1, aux, n);
        // 回退操作,不选index索引,找新的组合
        aux[index] = 0;
        // 回退时,未选index+1元素,递归
        findCombination(sum, index + 1, aux, n);
    }

    private static void showData(int[] aux, int n) {
        for (int i = 0; i < n; i++) {
            if (aux[i] == 1) {
                System.out.print(i + 1 + " ");
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int m = 8;
        int n = 20;
        int[] aux = new int[n];
        findCombination(m, 0, aux, n);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值