zoukankan      html  css  js  c++  java
  • 算法实现 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;
       
      }
     
    }

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

    不断的总结,才能不断的提高;不断的思考,才能不断的进步!
  • 相关阅读:
    虚方法(virtual)和抽象方法(abstract)的区别
    IT社区
    C#中动态加载和卸载DLL
    应用程序体系结构
    Enterprise Architect 7.0入门教程
    jQuery插件开发基础1
    asp.net页面事件执行顺序
    codesmith4.1破解版
    在Web.config配置文件中自定义配置节点
    小巧实用的节拍器软件FineMetronome介绍 原创
  • 原文地址:https://www.cnblogs.com/nzyjlr/p/2003635.html
Copyright © 2011-2022 走看看