使用遞迴的反轉句子Go語言程式


在本教程中,我們將學習如何使用Go程式語言中的遞迴反轉句子。

遞迴是指函式透過直接或間接方式呼叫自身。每個遞迴函式都有一個基例或基準條件,它是遞迴中最終的可執行語句,並停止進一步的呼叫。遞迴將持續進行,直到滿足某些條件以阻止它。

以下是展示兩種不同型別遞迴(直接和間接)的兩個示例。

使用直接遞迴方法使用遞迴反轉句子

語法

Func recursion() {
   recursion(); /* function calls itself */
}
func main() {
   recursion();
}

演算法

  • 步驟1 - 匯入fmt包

  • 步驟2 - 建立函式reversesentence()

  • 步驟3 - 使用if條件執行程式碼

  • 步驟4 - 函式自身遞迴呼叫

  • 步驟5 - 啟動main()函式

  • 步驟6 - 呼叫reversesentence()函式

  • 步驟7 - 使用fmt.Print()列印結果。

示例

使用直接遞迴方法使用遞迴反轉句子的Go語言程式程式碼。

// GOLANG PROGRAM TO REVERSE A SENTENCE USING RECURSION package main // fmt package provides the function to print anything import "fmt" // create the function reversesentence() func reversesentence(input string) { if len(input) == 0 { return } // recursive call of the function itself reversesentence(input[1:]) fmt.Print(string(input[0])) } func main() { fmt.Println("GOLANG PROGRAM TO REVERSE A SENTENCE USING RECURSION") // calling the function reversesentence() fmt.Println("Entered sentence =") var sentence string sentence = "Taylor Swift is the best" fmt.Println(sentence) reversesentence(sentence) // Print the result }

輸出

GOLANG PROGRAM TO REVERSE A SENTENCE USING RECURSION
Entered sentence =
Taylor Swift is the best
tseb eht si tfiwS rolyaT

程式碼描述

  • 在上面的程式中,我們首先宣告main包

  • 我們匯入了包含fmt包檔案的fmt包

  • 接下來,我們建立一個函式reversesentence(),使用遞迴技術反轉句子

  • 我們將使用if條件語句,如果條件為真則執行一段程式碼,如果為假則遞迴呼叫自身函式

  • 現在啟動main()函式

  • 現在呼叫reversesentence()函式

  • 最後使用fmt.Print()在螢幕上列印反轉後的句子。

使用間接遞迴方法使用遞迴反轉句子

語法

func recursion_1() {
   recursion_2()
}
func recursion_2(){
   recursion_1()
}
func main() {
   recursion_1();
}

演算法

  • 步驟1 - 匯入fmt包

  • 步驟2 - 建立函式reverse()

  • 步驟3 - 使用if條件執行程式碼

  • 步驟4 - 遞迴呼叫函式reverse2()

  • 步驟5 - 建立函式reverse2()

  • 步驟6 - 間接遞迴呼叫函式reverse()

  • 步驟7 - 啟動main()函式

  • 步驟8 - 呼叫函式reverse()

  • 步驟9 - 使用fmt.Print()在螢幕上列印結果

示例

使用間接遞迴方法使用遞迴反轉句子的Go語言程式程式碼

// GOLANG PROGRAM TO REVERSE A SENTENCE USING RECURSION package main // fmt package provides the function to print anything import "fmt" // create the function reverse() func reverse(input string) { if len(input) == 0 { return } // recursive call of the function reverse2 reverse2(input[1:]) fmt.Print(string(input[0])) } func reverse2(n string) { if len(n) == 0 { return } // recursive call of the function the first function indirectly reverse(n[1:]) fmt.Print(string(n[0])) } func main() { fmt.Println("GOLANG PROGRAM TO REVERSE A SENTENCE USING RECURSION") // calling the function reverse() var sentence string sentence = "Golang Solutions" fmt.Println("Entered Sentence\n",sentence) reverse2(sentence) // Print the result }

輸出

GOLANG PROGRAM TO REVERSE A SENTENCE USING RECURSION
Entered Sentence
Golang Solutions
snoituloS gnaloG

程式碼描述

  • 在上面的程式中,我們首先宣告main包

  • 我們匯入了包含fmt包檔案的fmt包

  • 接下來,我們建立一個函式reverse(),使用遞迴技術反轉句子

  • 我們將使用if條件語句,如果條件為真則執行一段程式碼,如果為假則遞迴呼叫第二個函式reverse2()

  • 接下來,我們建立一個函式reverse2()。這裡間接地對第一個函式進行遞迴函式呼叫,該函式間接呼叫第一個函式reverse()

  • 現在啟動main()函式。GO程式的執行從main()函式開始

  • 接下來,我們呼叫reverse()函式來反轉句子。

  • 最後,使用內建函式fmt.Print()在螢幕上列印句子的反轉結果。此函式在fmt包下定義,有助於寫入標準輸出。

結論

在以上兩個示例中,我們已成功編譯並執行了使用遞迴技術的Go語言程式程式碼來反轉句子。我們展示了直接和間接型別的遞迴方法。在第一個示例中,我們展示了直接遞迴方法,在第二個示例中,我們展示了間接遞迴方法。

更新於:2022年10月25日

瀏覽量:556

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告