使用递归查找两个数字乘积的 Golang 程序


两个数字的乘积是将它们相乘得到的结果。因此,15 是 3 和 5 的乘积,22 是 2 和 11 的乘积,依此类推。

递归是指函数通过直接或间接的方式调用自身。每个递归函数都具有一个基本情况或基本条件,它是递归中最终的可执行语句,并停止进一步的调用。

示例-1:使用直接递归方法查找两个数字乘积的 Golang 程序代码

语法

Syntax for direct recursion
func recursion() {
   recursion()
}
func main() {
   recursion();
}

算法

  • 步骤 1 - 导入 fmt 包。

  • 步骤 2 - 创建 product() 函数。

  • 步骤 3 - 我们将使用 if...else 条件语句。

  • 步骤 4 - 开始 main() 函数。

  • 步骤 5 - 初始化整数变量。

  • 步骤 6 - 调用 product() 函数。

  • 步骤 7 - 使用 fmt.Printf() 在屏幕上打印结果。

示例

package main  
// fmt package provides the function to print anything
import "fmt"  

func product(n int, m int) int {
   // if n less than m
   if n < m {
      return product(m, n)
   } else {
      // if m is not equal to 0      
      if (m != 0) {
         // recursive call to itself 
         return (n + product(n, m-1))
      } else {
         return 0
      }
   }
}
// Start the function main()
func main() {
  // Declare and initialize integer variables  
   var a int
   var b int
   var c int
   a = 4
   b = 15
   fmt.Println("The N1=",a,"\nThe N2 =",b)
   c = product(a, b)

   fmt.Printf("The product of two numbers: %d\n", c)
}

输出

The N1= 4 
The N2 = 15
The product of two numbers: 60

描述

  • 在上面的程序中,我们首先声明了 main 包。

  • 我们导入了包含 fmt 包文件的 fmt 包。

  • 接下来,我们创建一个 product() 函数,使用递归技术查找两个数字的乘积。

  • 我们将使用 if-else 条件语句,它允许您在指定条件为真时执行一段代码,而在条件为假时执行另一段代码。

  • 如果 n 小于 m,则函数将返回 product(m, n) 并结束递归函数。

  • 如果 m 不等于 0,则函数将递归调用自身并返回 (n + product(n, m-1))。

  • 现在开始 main() 函数。

  • 接下来初始化整数变量 a 和 b,它们对应于需要查找乘积的两个数字。整数 c 对应于最终结果。

  • 现在调用 product() 函数

  • 并使用 fmt.Printf() 在屏幕上打印结果。

示例-2 使用间接递归方法查找两个数字乘积

语法

Syntax for indirect recursion
func recursion_1() {
   recursion_2()}
func recursion_2(){
   recursion_1()}
func main() {
   recursion_1();
}

算法

  • 步骤 1 - 导入 fmt 包。

  • 步骤 2 - 创建 product_1() 函数。

  • 步骤 3 - 我们将使用 if...else 条件语句。

  • 步骤 4 - 创建 product_2() 函数。

  • 步骤 5 - 间接递归调用 product_1() 函数。

  • 步骤 5 - 开始 main() 函数。

  • 步骤 6 - 初始化整数变量 x、y 和 z。

  • 步骤 7 - 调用 product_1() 函数。

  • 步骤 8 - 使用 fmt.Printf() 在屏幕上打印结果。

示例

package main  
// fmt package provides the function to print anything
import "fmt"  

// defining the function with a parameter of int
// type and have a return type int
func product_1(n int, m int) int {
   
   // this is the base condition
    // if n less than m
   if n < m {
      return product_2(m, n)
   } else {
      // if m is not equal to 0      
      if (m != 0) {
         // recursive call to the second function product_2()
         return (n + product_2(n, m-1))
      } else {
         return 0
      }
   }
}
func product_2(n int, m int) int {
   
   // if n less than m
   if n < m {
      return product_1(m, n)
   } else {
      // if m is not equal to 0      
      if (m != 0) {
         // recursive call to the first function product_1() 
         return (n + product_1(n, m-1))
      } else {
         return 0
      }
   }
}
func main() {
  // Declare and initialize integer variable  
   var x int
   var y int
   var z int
   x = 11
   y = 15
   fmt.Println("The N1=",x,"\nThe N2 =",y)
   z = product_1(x, y)
 
   fmt.Printf("The product of two numbers: %d\n", z)
}

输出

The N1= 11 
The N2 = 15
The product of two numbers: 165

代码描述

  • 在上面的程序中,我们首先声明了 main 包。

  • 我们导入了包含 fmt 包文件的 fmt 包。

  • 接下来,我们创建一个 product_1() 函数,使用递归技术查找两个数字的乘积。

  • 我们将使用 if-else 条件语句,它允许您在指定条件为真时执行一段代码,而在条件为假时执行另一段代码。

  • 如果 n 小于 m,则函数将返回 product(m, n) 并结束递归函数。

  • 如果 m 不等于 0,则函数将间接递归调用第二个函数 product_2() 并返回 (n + product(n, m-1))。

  • 接下来,我们创建一个 product_2() 函数,类似于上述函数,对 product_1() 函数进行递归调用并返回 (n + product_1(n, m-1))。

  • 现在开始 main() 函数。

  • 接下来初始化整数变量 x 和 y,它们对应于需要查找乘积的两个数字。整数 z 对应于最终结果。

  • 现在调用第一个函数 product_1()。

  • 最后,使用 fmt.Printf() 在屏幕上打印结果。

更新于: 2022-12-29

244 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.