首頁  >  文章  >  後端開發  >  盜賊跨越牆壁所需的跳躍次數

盜賊跨越牆壁所需的跳躍次數

PHPz
PHPz轉載
2023-09-11 20:57:021201瀏覽

盜賊跨越牆壁所需的跳躍次數

想像一個囚犯(或小偷)想要從監獄逃脫。為了做到這一點,他需要越過 N 個長度不同的牆。他每次跳躍可以爬升 X 英尺。但是,由於牆壁很滑,他每次跳躍後都會下滑 Y 英尺。因此,我們需要計算穿越所有牆壁所需的跳躍次數。在本文中,我們將探討不同的C 技術,以找到逃脫監獄所需的跳躍次數。

輸入輸出場景

我們以陣列的形式有不同高度的 N 面牆。 X 是跳躍長度,而Y 是他後退的長度。我們有跳躍次數作為輸出。

Input: height[] = {5, 18, 10, 3}
       N = 4, X = 5, Y = 2
Output: 11
Input: height[] = {15, 8, 10, 3, 5, 12}
       N = 6, X = 5, Y = 2
Output: 16

使用迭代方法

在這裡,我們使用forwhile迴圈來找到跳躍次數。

當牆的高度小於跳躍長度(x)時,可以單次跳躍越過牆。因此,numJumps增加一。我們使用 continue 語句來停止剩餘的迴圈並繼續進行下一個迴圈。

當高度大於跳躍長度時,我們使用while循環透過h – (x – y)計算跳躍次數,直到剩餘高度變小大於或等於跳躍長度。

接下來,我們為最後一面牆添加一次跳躍。

Example

的中文翻譯為:

範例

#include <iostream>
using namespace std;

int numOfJumps(int x, int y, int N, int heights[]) {
   int numJumps = 0;

   // When the height is less than jump length
   for (int j = 0; j < N; j++) {
      if (x >= heights[j]) {
         numJumps++;
         continue;
      }

      // When the height is more than jump length
      int h = heights[j];
      while (h > x) {
         numJumps++;
         h = h - (x - y);
      }
      numJumps++;
   }
   return numJumps;
}

int main() {
   int N = 5; // Number of walls
   int x = 4; // jump height
   int y = 1; // length after he slips back
   int heights[] = {5, 18, 10, 3, 5};
   int minJumpsRequired = numOfJumps(x, y, N, heights);
   cout << "Minimum number of jumps required: " << minJumpsRequired << endl;
   return 0;
}

輸出

Minimum number of jumps required: 14

使用直接計算

以下是計算小偷越過牆壁所需跳躍次數的公式 -

Jumps = ceil((h - y) / static_cast<double>(x - y))

我們使用一個for迴圈來遍歷每堵牆。當前牆的高度儲存在變數h中。

然後,我們用公式直接計算出所需的跳躍次數。我們使用 ceil 函數將數值四捨五入到最接近的整數。

Example

的中文翻譯為:

範例

#include <iostream>
#include <cmath>
using namespace std;

int numOfJumps(int x, int y, int N, int height[]) {
    int numJumps = 0;
    
    for (int j = 0; j < N; j++) {
        int h = height[j];
        int jumpsRequired = ceil((h - y) / static_cast<double>(x - y));
        numJumps += jumpsRequired;
    }
    
    return numJumps;
}


int main() {
    int x = 8, y = 2;
    int height[] = { 4, 14, 8, 16, 20, 11 };
    int N = sizeof(height) / sizeof(height[0]);
    
    int minJumpsRequired = numOfJumps(x, y, N, height);
    cout << "Minimum number of jumps required: " << minJumpsRequired << endl;
    return 0;
}

輸出

Minimum number of jumps required: 12

使用除法和模運算子

我們也可以使用 除法 (/)取模 (%) 運算子來計算跳躍的次數。在這裡,我們計算牆的高度與跳躍長度之間的差異。如果差值大於0,我們將其除以 (x-y) 來計算跳躍次數。如果有餘數,我們就加一。而如果差值為零或負數,則我們只需要一次跳躍。

Example

的中文翻譯為:

範例

#include <iostream>

using namespace std;

int numOfJumps(int x, int y, int N, int height[]) {
   int jumps = 0;
   for (int j = 0; j < N; j++) {
      int diff = height[j] - x;

      // When height is greater than jump length
      if (diff > 0) {
         jumps++;

         // Additional jumps
         jumps += diff / (x - y);

         // If there is a remainder, increment the jumps
         if (diff % (x - y) != 0)
            jumps++;
      }

      // When height is less than jump length
      else {
         jumps++;
      }
   }
   return jumps;
}
int main() {
   int N = 5; // Number of walls
   int x = 5; // jump height
   int y = 2; // length after he slips back
   int height[] = { 15, 8, 10, 3, 5, 12};
   int minJumpsRequired = numOfJumps(x, y, N, height);
   cout << "Minimum number of jumps required: " << minJumpsRequired << endl;
   return 0;
}

輸出

Minimum number of jumps required: 12

結論

我們討論了各種方法來確定小偷越牆的跳躍次數。我們可以使用迭代方法。我們可以直接使用公式來取代這樣的迭代。另外,我們可以使用除法取模運算子來解決這個問題。

以上是盜賊跨越牆壁所需的跳躍次數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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