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;
       
      }
     
    }

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

    不断的总结,才能不断的提高;不断的思考,才能不断的进步!
  • 相关阅读:
    Linux 安装 iptables防火墙
    CentOS最常用命令及快捷键整理
    WebAPI 和 webservice接口
    Linux 文件权限
    Linux查看系统信息的一些命令及查看已安装软件包的命令
    navicat连接虚拟机(centos)中的mysql
    Nmap扫描与Tcpdump抓包分析
    python 识别验证码自动登陆
    iptables开通某些端口
    hive的安装和使用
  • 原文地址:https://www.cnblogs.com/nzyjlr/p/2003635.html
Copyright © 2011-2022 走看看