使用遞迴查詢兩個數字乘積的 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() 在螢幕上列印結果。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP