如何在Go语言中执行nCr(r-组合)?


在本教程中,我们将使用Go编程语言执行nCr(r-组合)。nCr(r-组合)的用例是找到不考虑顺序的所有可能排列的总数。换句话说,我们从n个项目中选择r个项目,而顺序无关紧要。本教程将包括两种在Go编程语言中找到此结果的方法。

解释

例如,对于n = 5和r = 3

nCr = n! / ( r! * ( n - r )! )

= 5! / ( 3! * 2! )

= 120 / 12

= 10

算法

步骤1 - 声明所有需要的变量来存储n、r、n的阶乘、r的阶乘和n-r的阶乘。

步骤2 - 初始化n、r、n的阶乘、r的阶乘和n-r的阶乘的值。

步骤3 - 找到n、r和n-r的阶乘。

步骤4 - 使用上述公式计算nCr。

步骤5 - 打印结果。

示例

在这个例子中,我们将使用for循环来计算nCr。

package main

// fmt package provides the function to print anything
import (
   "fmt"
)
func main() {
   
   // declaring the variables to store the value of n, r and answer
   var n, r, nFactorial, rFactorial, nminusrFactorial, answer int
   fmt.Println("Program to find the nCr using the for loop.")
   
   // initializing the value of n 
   n = 10
   
   // initializing the value of r
   r = 8
   nFactorial = 1
   
   // finding factorial of n
   for i := 1; i <= n; i++ {
      nFactorial = nFactorial * i
   }
   rFactorial = 1
   
   // finding factorial of r
   for i := 1; i <= r; i++ {
      rFactorial = rFactorial * i
   }
   nminusrFactorial = 1
   
   // finding factorial of n - r
   for i := 1; i <= n-r; i++ {
      nminusrFactorial = nminusrFactorial * i
   }
   
   // finding answer by using the formulae
   answer = nFactorial / (rFactorial * nminusrFactorial)
   
   // printing the result
   fmt.Println("The value of nCr with n =", n, "and r=", r, "is", answer)
}

输出

Program to find the nCr using the for loop.
The value of nCr with n = 10 and r= 8 is 45

算法

步骤1 - 声明所有需要的变量来存储n、r、n的阶乘、r的阶乘和n-r的阶乘。

步骤2 - 初始化n、r、n的阶乘、r的阶乘和n-r的阶乘的值。

步骤3 - 在单独的函数中找到n、r和n-r的阶乘。

步骤4 - 使用上述公式计算nCr。

步骤5 - 打印结果。

示例

在这个例子中,我们使用单独的函数来找到n、r和n-r的阶乘,从而计算nCr。

package main

// fmt package provides the function to print anything
import (
   "fmt"
)

// this is a recursive function of return type int

// which is returning the factorial of number

// passed in argument
func factorial(n int) int {
   if n == 1 {
      return 1
   }
   return factorial(n-1) * n
}
func main() {
   
   // declaring the variables to store the value of n, r and answer
   var n, r, nFactorial, rFactorial, nminusrFactorial, answer int
   fmt.Println("Program to find the nCr using the separate function to find the factorial of n, r and, n-r.")
   
   // initializing the value of n
   n = 10
   
   // initializing the value of r
   r = 8
   
   // finding factorial of n
   nFactorial = factorial(n)
   
   // finding factorial of r
   rFactorial = factorial(r)
   
   // finding factorial of n - r 
   nminusrFactorial = factorial(n - r)
   
   // finding answer by using the formulae
   answer = nFactorial / (rFactorial * nminusrFactorial)
   
   // printing the result
   fmt.Println("The value of nCr with n =", n, "and r=", r, "is", answer)
} 

输出

Program to find the nCr using the separate function to find the factorial of n, r and, n-r.
The value of nCr with n = 10 and r= 8 is 45

结论

这是在Go编程语言中执行nCr(r-组合)的两种方法。第二种方法在模块化和代码可重用性方面更好,因为我们可以在项目的任何地方调用该函数。要了解更多关于Go的信息,您可以浏览这些教程。

更新于:2022年11月29日

451 次浏览

启动你的职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.