输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数, 使其和等于 m ,要求将其中所有的可能组合出来---经典数据结构第21道
2016-11-17 22:17
519 查看
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 ; 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 ; findCombination(m, 0, aux, n); } }
相关文章推荐
- 输入两个整数n 和m,从数列1,2,3.......n 中随意取几个数, 使其和等于m ,要求将其中所有的可能组合列出来
- 21.输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来.
- <仅是自己做笔记。。。系列-13>输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数, 使其和等于 m ,要求将其中所有的可能组合列出来.
- 输入两个整数n和m,从数列1,2,3....n中随意取几个数,使其和等于m,要求将其中所有的可能组合列出来
- 输入两个整数n和m,从数列1,2.......n中随意取几个数,使其和等于m,要求将其中所有的可能组合列出来
- 输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数, 使其和等于 m ,要求将其中所有的可能组合列出来.
- 输入两个整数 n 和 m,从数列1,2,3…….n 中随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来
- PHP 输入两个整数n 和m,从数列1,2,3.......n 中随意取几个数, 使其和等于m ,要求将其中所有的可能组合列出来
- 输入两个整数 n 和 m,从数列1,2,3.......n 中随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来
- 第21 题 2010 年中兴面试题 编程求解: 输入两个整数n 和m,从数列1,2,3.......n 中随意取几个数, 使其和等于m ,要求将其中所有的可能组合列出来.
- 输入两个整数n 和m,从数列1,2,3.......n 中随意取几个数,使其和等于m ,要求将其中所有的可能组合列出来.
- 输入两个整数n 和m,从数列1,2,3.......n 中随意取几个数, 使其和等于m ,要求将其中所有的可能组合列出来
- 输入两个整数 n 和 m,从数列1,2,3.......n 中随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来.
- 输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数, 使其和等于 m ,要求将其中所有的可能组合列出来.
- 输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数, 使其和等于 m ,要求将其中所有的可能组合列出来.
- 输入两个整数 n 和 m,从数列1,2,3.......n 中随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来.
- 输入两个整数n 和m,从数列1,2,3.......n 中随意取几个数, 使其和等于m ,要求将其中所有的可能组合列出来.
- 输入两个整数n和m,从数列1,2,3.......n 中随意取几个数, 使其和等于m,要求将其中所有的可能组合列出来。
- 输入两个整数 n 和 m,从数列 1,2,3.......n 中 随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来
- 输入两个整数 n 和 m,从数列1,2,3.......n 中随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来.