Java 程序:计算C(n, r)

原文:https://www.studytonight.com/java-programs/java-program-to-perform-ncr

在本教程中,我们将学习如何找到 C(n, r) 的价值。C(n, r) 值给出了从 n 个对象中选择 r 个对象的方法,不考虑顺序;更正式地说,n 元素集的 r 元素子集(或 r 元素组合)的数量。但是在继续之前,如果你不熟悉 java 中循环的概念,那么一定要查看关于 Java 中循环的文章。

以下公式用于计算 C(n, r) 值。

C(n, r) = (n!)/((n-r)!* r!)

输入:输入 n: 5 的值

输入 r: 2 的值

输出: 5C2 = 10

上述问题出现了两种情况:

情况 1:当值由用户定义时

情况 2:当值被预定义时

让我们分别看看这些案例。

程序 1:在 Java 中找到 C(n, r)

在这个程序中,当值是用户定义的时,我们将找到 C(n, r) 值。这意味着,首先我们将要求用户输入 n 和 r 值,然后我们将使用公式计算 C(n, r) 值。这里,我们将使用 for 循环来计算阶乘。

算法:

  1. 开始

  2. 声明变量。

  3. 要求用户初始化变量。

  4. 检查是否有可能找到 C(n, r) 值。

  5. 如果可能,那么调用一个方法来计算 C(n, r)

  6. 使用 for 循环计算一个数的阶乘。

  7. 使用公式求出 C(n, r) 值。

  8. 请输入 C(n, r) 值。

  9. 如果无法计算 C(n, r) 值,则输入 n 和 r 的值,使 n>=r。

  10. 停止

下面的例子说明了上述算法的实现。

//Java Program to find the `C(n, r)`
import java.util.*;  
public class Main 
{   
    //Method to calculate the `C(n, r)` value
    static int `C(n, r)`(int n, int r)   
    {   
          return fact(n) / (fact(r) * fact(n - r));   
    }   
    //Method to calculate the factorial of the number
    static int fact(int n)   
    {   
          int res = 1;   
          for (int i = 2; i <= n; i++)   
          res = res * i;   
          return res;   
    }   
   public static void main(String[] args)   
   {   
       //Take input from the variables
       //Create instance of the Scanner Class
       Scanner sc = new Scanner(System.in);  
       int n,r;  //Declare variables
       System.out.println("Enter the value of n :");  
       n = sc.nextInt();  //Initialize the variables
       System.out.println("Enter the value of r :");
       r = sc.nextInt();  //Initialize the variables
       if(n>=r)
       {
           //Print the `C(n, r)` value
            System.out.println("Value of "+ n+"C"+r+"= "+`C(n, r)`(n, r)); 
       }
        else
          System.out.println("n value should be greater than or equals to r value");
   }   
}

输入 n: 5 的值 输入 r: 2 的值 的值 5C2= 10

程序 2:在 Java 中找到 C(n, r)

在这个程序中,当数值在程序中被预定义时,我们将找到 C(n, r) 值。

算法:

  1. 开始

  2. 声明并初始化变量。

  3. 检查是否有可能找到 C(n, r) 值。

  4. 如果可能,那么调用一个方法来计算 C(n, r)

  5. 计算数字的阶乘。

  6. 使用公式求出 C(n, r) 值。

  7. 请输入 C(n, r) 值。

  8. 如果无法计算 C(n, r) 值,则输入 n 和 r 的值,使 n>=r。

  9. 停止

下面的例子说明了上述算法的实现。

//Java Program to find the `C(n, r)`
public class Main 
{   
    //Method to calculate the `C(n, r)` value
    static int `C(n, r)`(int n, int r)   
    {   
          return fact(n) / (fact(r) * fact(n - r));   
    }   
    //Method to calculate the factorial of the number
    static int fact(int n)   
    {   
          int res = 1;   
          for (int i = 2; i <= n; i++)   
          res = res * i;   
          return res;   
    }   
   public static void main(String[] args)   
   {   
       int n=7,r=2;  //Declare and initialize the variables
       System.out.println("The entered value of n is :"+ n);  
       System.out.println("The entered value of r is :"+ r);
        if(n>=r)
       {
           //Print the `C(n, r)` value
            System.out.println("Value of "+ n+"C"+r+"= "+`C(n, r)`(n, r)); 
       }
        else
          System.out.println("n value should be greater than or equals to r value");

   }   
}

输入的 n 值为:7 输入的 r 值为:2 值为 7C2= 21

程序 3:在 Java 中找到 C(n, r)

在这个程序中,当值是用户定义的时,我们将找到 C(n, r) 值。这意味着,首先我们将要求用户输入 n 和 r 值,然后我们将使用公式计算 C(n, r) 值。这里,我们将使用 while 循环来计算阶乘。

算法:

  1. 开始

  2. 声明变量。

  3. 要求用户初始化变量。

  4. 检查是否有可能找到 C(n, r) 值。

  5. 如果可能,那么调用一个方法来计算 C(n, r)

  6. 使用 while 循环计算一个数的阶乘。

  7. 使用公式求出 C(n, r) 值。

  8. 打印 C(n, r) 值。

  9. 如果无法计算 C(n, r) 值,则输入 n 和 r 的值,使 n>=r。

  10. 停止

下面的例子说明了上述算法的实现。

//Java Program to find `C(n, r)`
import  java.util.*;
public class Main
{
    //Calculate factorial of the number
    static double fact(double n)
    {
        int i=1;
        double fact=1;
        while(i<=n)
        {
              fact=fact*i;
           i++;
         }
          return fact;
    }
    //Calculate the combination value
    static double combination(int n,int r)
    {
        double com=fact(n)/(fact(n-r)*fact(r));
        return com;
    }
    //Driver Code
    public static void main(String arg[])    
    {
        //Take input from the user
        //Create an instance of the Scanner class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the value of n : ");
        int n=sc.nextInt();
        System.out.println("Enter the value of r : ");
        int r=sc.nextInt();
        //Check whether it is possible to find the `C(n, r)` value.
        if(n>=r)
        {
           System.out.println("The value of "+n+"c"+r+" is : "
             +combination(n,r));
        }
        else
          System.out.println("n value should be greater than or equals to r value");

    }    
}

输入 n: 8 的值 输入 r: 3 的值 8 C3 的值为:56.0