zoukankan      html  css  js  c++  java
  • 递归实现C(m,n)从m个数中取出n个

    从M个数中任一取出N个数,并打印结果,
    思路:
    假设从5个数中任意取3个;相当于从5个数中取出1个后,在剩下的4个里任取2个;
    而从4个里任取2个又相当于从4个里取出1个后在剩下的3个里任取1个。

    package res;
    
    public class Cmn {
    	// 从m个数中取n个数
    	static int[] RES;//用来保存取出的结果
    
    	public static void main(String[] args) {
    		C(13, 5);
    	}
    
    	public static void C(int m, int n) {
    		RES = new int[n];
    		func(m, n);
    	}
    
    	public static void func(int m, int n) {
    		int i, j;
    		for (i = n; i <= m; i++) {
    			RES[n - 1] = i;
    			if (n > 1) {
    				func(i - 1, n - 1);
    			} else {
    				for (j = 0; j < RES.length; j++)
    					System.out.print(RES[j] + "  ");
    				System.out.println();
    			}
    		}
    	}
    }
    
    
  • 相关阅读:
    CF1270H
    CF1305G
    LeetCode-Sqrt(x)
    LeetCode-Plus One
    LeetCode-Integer to Roman
    LeetCode-Roman to Integer
    LeetCode-String to Integer (atoi)
    LeetCode-Reverse Integer
    C++
    LeetCode-Gray Code
  • 原文地址:https://www.cnblogs.com/zhang-han/p/14019042.html
Copyright © 2011-2022 走看看