zoukankan      html  css  js  c++  java
  • practice01

    1. 组合数公式: C(n, k) =C(n-1, k) +C(n-1, k-1) 要求利用该公式写递归函数求组合数。

    #include <stdio.h>
    
    int C(int a,int b)
    {
        if(a==b||b==0)
            return 1;
        else
            return C(a-1,b)+C(a-1,b-1);
    }
    int main()
    {
        int n,k;
        int s;
        while(scanf("%d%d",&n,&k)!=EOF)
        {
           s=C(n,k);
           printf("%d
    ",s);
        }
        return 0;
    }
    --------------------- 
    原文:https://blog.csdn.net/rating_/article/details/84034728 
    View Code
    # -*- coding:utf-8 -*-
    # 用python实现排列组合C(n,m) = n!/m!*(n-m)!
    def get_value(n):
        if n==1:
            return n
        else:
            return n * get_value(n-1)
    
    def gen_last_value(n,m):
         first = get_value(n)
         print("n:%s     value:%s"%(n, first))
         second = get_value(m)
         print("n:%s     value:%s"%(m, second))
         third = get_value((n-m))
         print("n:%s     value:%s"%((n-m), third))
         return first/(second * third)
    
    
    if __name__ == "__main__":
        # C(12,5)
        rest = gen_last_value(5,3)
        print("value:", rest)
    # 本方法的流程:(5,3),first=5*4*3*2*1;second=3*2*1;third=first/second
    View Code
    # -*- coding:utf-8 -*-
    # 用python实现排列组合C(n,m) = n!/m!*(n-m)!
    def get_value(n):
        if n==b:
            return n
        else:
            return n * get_value(n-1)
    def get_value_m(m):
        if m==1:
            return m
        else:
            return m * get_value_m(m-1)
    
    def gen_last_value(n,m):
         first = get_value(n)
         print("n:%s     value:%s"%(n, first))
         second = get_value_m(m)
         print("n:%s     value:%s"%(m, second))
         return first/second
    
    if __name__ == "__main__":
        n = int(input('num n:'))
        m = int(input('num m:'))
        b = n-m+1
        rest = gen_last_value(n,m)
        print("value:", rest)
    #举例:C(5,3);get_value(n)输出:5*4*3】【get_value_m输出:3*2*1】【gen_last_value=first/second
    升级版
  • 相关阅读:
    【LeetCode】297. 二叉树的序列化与反序列化
    【剑指Offer】面试题37. 序列化二叉树
    【剑指Offer】面试题59
    【剑指Offer】面试题57
    趣图:向客户介绍的产品VS实际开发的产品
    Spring AOP的实现机制
    Mysql高性能优化规范建议
    JavaScript八张思维导图
    深入 Nginx:我们是如何为性能和规模做设计的
    教你用认知和人性来做最棒的程序员
  • 原文地址:https://www.cnblogs.com/chengxiaofeng/p/10778846.html
Copyright © 2011-2022 走看看