遞迴練習題及解答


在本文中,我們將討論一些遞迴練習題及其詳細解答。

讓我們首先了解什麼是遞迴以及它是如何工作的

遞迴 − 遞迴是一種程式設計技術,其中函式或方法多次呼叫自身以解決問題。該函式將問題分解成更小的子問題,並依次解決這些子問題,直到達到基本情況。

基本情況是一個停止條件,它確保函式在有限時間內停止自身呼叫並返回結果。

遞迴是解決複雜問題的強大技術,但重要的是要仔細設計它以避免無限迴圈並確保函式正確終止,因為遞迴會多次呼叫函式。

問題1

這是與遞迴相關的最基本的問題。

使用階乘的概念查詢給定數字的階乘。

C++實現

#include <bits/stdc++.h>
using namespace std;
// recursive function to 
// calculate factorial of number
int Numberfact(int number) {
// base condition
    if(number == 1) {
        return 1;
    } else {
        return number * Numberfact(number-1);
    }
}
// main code
int main() {
   int number = 5;
   cout<< " The factorial of 5 is " << Numberfact(number);
   return 0;
}

輸出

The factorial of 5 is 120

問題2

在這個問題中,我們需要列印從 1 開始的系列的第 n 個數字,其中第 i 個數字是其前兩個數字的和,通常稱為斐波那契數列。

C++實現

#include <bits/stdc++.h>
using namespace std;
// function to 
// calculate nth number of
// Fibonacci series
int Numberfib(int number) {
   // base condition
   if(number <= 1) {
      return number;
   } else {
      return Numberfib(number-1)+Numberfib(number-2);
   }
}
// main code
int main() {
   int number = 9;
   cout<< " The 9th number of the Fibonacci series is " << Numberfib(number);
   return 0;
}

輸出

The 9th number of the Fibonacci series is 34

問題3

計算給定數字中數字的總和

C++實現

#include <bits/stdc++.h>
using namespace std;
// recursive method to 
// calculate sum of digits
int Sumofdigits(int number) {
// base case
   if(number <=10) {
      return number;
   }
   else {
      return number%10 + Sumofdigits( number/10 );
   }
}
// main code
int main() {
   int number = 563;
   cout<< " The sum of digits of the number " << number << " is "<< Sumofdigits(number);
   return 0;
}

輸出

The sum of digits of the number 563 is 14

問題4

計算一個數的“power”次冪的值。

在這個問題中,我們將得到兩個數字“number”和“power”,我們的任務是找到“number”的“power”次冪。

C++實現

#include <bits/stdc++.h>
using namespace std;
// recursive method to 
// generate the nth power
// of a given number
int powerofx( int nums , int pow) {
   // termination condition
   if(pow == 0) {
      return 1;
   } else {
      return nums*powerofx(nums, pow-1);
   }
}
// main code
int main() {
   int nums = 2;
   int pow =6;
   cout<< " The number " << nums << " To the power "<< pow <<" is "<< powerofx(nums, pow);
   return 0;
}

輸出

The number 2 To the power 6 is 64

問題5

求兩個數的最大公約數 (GCD)。

GCD(最大公約數)是可以除以一組兩個或多個數字而沒有任何餘數的最大數字。它也稱為這些數字的最高公因子 (HCF)。

假設我們有兩個不同的數字,14 和 28。

14 的因子是 1、2、7 和 14。

28 的因子是 1、2、4、7、14 和 28。

然後我們可以識別兩個數字共有的因子,它們是 1、2、7 和 14。可以除以 14 和 28 而沒有餘數的最大數字是 14,因此 14 和 28 的最大公約數是 14。

C++實現

#include <bits/stdc++.h>
using namespace std;
// function to recursively
// calculate the gcd
int greatestcommondivisor(int num1, int num2) {
   if (num2 == 0) {
      return num1;
   } else {
      return greatestcommondivisor(num2, num1 % num2);
   }
}
// main code
int main() {
   int num1 = 36;
   int num2 =60;
   cout<< " The Greatest common divisor of " << num1 << " and "<< num2<<" is "<< greatestcommondivisor(num1, num2);
   return 0;
}

輸出

The Greatest common divisor of 36 and 60 is 12

問題6

反向列印陣列

我們得到一個包含 n 個整數的陣列,我們的任務是按照第一個數字作為最後一個數字、第二個數字作為倒數第二個數字等等的順序列印它。

C++實現

#include <bits/stdc++.h>
using namespace std;
// recursive function to 
// =reverse print the given array
void reverseprint(int nums[], int begining, int end) {
   if (begining >= end) {
      return ;
   } else {
      cout << nums[end-1] << " ";
      reverseprint(nums, begining, end - 1);
   }
}
// main code
int main() {
   int size =4;
   int nums[] = { 2, 3, 4, 5 } ;
   cout<< " the given array is reverse order is " << endl ;
   reverseprint(nums, 0, size);
   return 0;
}

輸出

the given array is reverse order is 
5 4 3 2 

下面是一些更基本的練習題,可以幫助你掌握遞迴的基礎知識:

編寫一個函式來遞迴地檢查字串是否為迴文。

編寫一個函式使用尾遞迴查詢給定數字的階乘。

編寫一個函式來解決漢諾塔難題。

編寫一個函式對排序陣列執行二分查詢。

更新於:2023年8月16日

2K+ 瀏覽量

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告