首頁  >  文章  >  後端開發  >  遞歸練習問題與解決方案

遞歸練習問題與解決方案

PHPz
PHPz轉載
2023-09-15 10:05:08729瀏覽

遞歸練習問題與解決方案

在本文中,我們將討論一些遞歸練習問題及其詳細解決方案。

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

遞歸 - 遞歸是一種程式設計技術,其中函數或方法多次呼叫自身以解決問題。該函數將問題分解為更小的子問題並解決它們,直到達到基本情況。

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

遞歸是解決複雜問題的強大技術,但仔細設計它以避免無限循環並確保函數在遞歸多次呼叫函數時正確終止非常重要。

問題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

計算一個數的「冪」次方的值。

在這個問題中,我們將給出兩個數字“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 

以下是一些更基礎的練習問題,以掌握遞歸的基本層次 -

寫一個函數來遞歸檢查字串是否為回文。

使用尾遞歸寫一個函數來找出給定數字的階乘。

寫一個函數來解決漢諾塔問題。

寫一個函數來對排序數組執行二分搜尋。

以上是遞歸練習問題與解決方案的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除