• 算法实现 C(n, k)


    Calling methods from within other methods

    ------------------------程序如下:-----------------------------

    /*
     * File: Combinations.java
     * -----------------------
     * This program computes the mathematical combinations function
     * C(n, k), which is the number of ways of selecting k objects
     * from a set of n distinct objects.
     */

    package chapter5;
    import java.util.Scanner;

    public class Combinations {

     public void run(){
      
        int n;
        int k;
        int result;
        System.out.println("please input the n:");
        Scanner input=new Scanner(System.in);
        n=input.nextInt();
        System.out.println("please input the k:");
        Scanner input1=new Scanner(System.in);
        k=input1.nextInt();
        result=Combinations(n,k);                                        // 调用函数Combinations(int n,int k)
        System.out.println("C("+n+","+k+")="+result);
      
     }
       // 计算C(n,k)=n!/(k!*(n-k)!)
      private static int Combinations(int n,int k){
           int result;
           result=factorial(n)/(factorial(k)*factorial(n-k));     // 调用函数factorial(int n)
           return result;
      }
      // 计算n!
      private static int factorial(int n) {
         int result=1;
         for(int i=1;i<=n;i++){
            result*=i;
       }
       return result;
       
      }
     
    }

    -------------------------运行程序-----------------

    不断的总结,才能不断的提高;不断的思考,才能不断的进步!
  • 相关阅读:
    BZOJ 3158: 千钧一发
    BZOJ 1677: [Usaco2005 Jan]Sumsets 求和
    BZOJ 1574: [Usaco2009 Jan]地震损坏Damage
    BZOJ 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课
    BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞
    BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花
    Vijos P1740聪明的质检员
    Vijos P1680距离
    Vijos P1067Warcraft III 守望者的烦恼
    BZOJ 1385: [Baltic2000]Division expression
  • 原文地址:https://www.cnblogs.com/nzyjlr/p/2003635.html
Copyright © 2020-2023  润新知