確定勝者所需的比賽場數


錦標賽有多種型別——單淘汰賽、雙淘汰賽、迴圈賽等。假設您是一位組織者,並且想知道根據遊戲規則進行錦標賽需要進行多少場比賽。

在本文中,我們將討論使用 C++ 查詢確定勝者所需的比賽場數的不同方法。

理解問題

在單淘汰賽中,我們有一系列比賽,其中每個隊伍或選手都與另一個隊伍或選手競爭。每場比賽都有兩個隊伍或選手。輸的一方將被淘汰。每一輪後,隊伍都會被淘汰,直到宣佈勝者。

  • 在雙淘汰賽中,我們有兩個類別。首先,所有隊伍之間都進行一次比賽。失敗者不會被淘汰,而是被移至失敗者組,在那裡他們互相競爭。同樣,勝者組之間也會進行比賽。

  • 最後,有一個總決賽,勝者組的勝者和失敗者組的勝者進行比賽。如果勝者組的勝者獲勝,那麼它就成為最終的勝者。而如果失敗者組的勝者獲勝,則比賽將再次舉行以決定勝者。

  • 在迴圈賽中,每個參與者都會與所有其他參與者比賽一次。獲勝次數最多的參與者成為最終的勝者。

我們必須找到這些型別的錦標賽確定勝者所需的比賽場數。

輸入輸出場景

假設您已知參加錦標賽的隊伍或選手的數量。輸出將給出確定勝者所需的比賽場數。

In case of single elimination
Input: N = 8
Output: 7
In case of double elimination
Input: N = 8
Output: 14
In case of league tournament
Input: N = 8
Output: 14

單淘汰賽

我們有 **N** 個隊伍或選手。如果 **N** 是 **偶數**,則每一輪需要 **N/2** 場比賽,並且 **N/2** 個選手或隊伍將進入下一輪。如果 **N** 是奇數,則第一輪需要 **N/2** 場比賽,而 **N/2** 個隊伍和一個隊伍將進入下一輪。

讓我們來看一個例子,其中有 14 個隊伍參加錦標賽。因此,比賽輪次如下:

  • 第一輪,14 個隊伍在 7 場比賽中相互競爭。7 個隊伍進入下一輪。

  • 第二輪,7 個隊伍在 3 場比賽中競爭。4 個隊伍進入下一輪。

  • 第三輪,4 個隊伍在 2 場比賽中競爭。2 個隊伍進入下一輪。

  • 2 個隊伍在決賽中競爭以獲得冠軍。

總比賽場數 = 7 + 3 + 2 + 1 = 13,即 **(N – 1)**。

因此,單淘汰賽中確定勝者所需的比賽場數為 **(N – 1)**

示例

讓我們來看一個例子:

#include <iostream>
using namespace std;
int numOfMatches(int N){
   int result = (N - 1);
   return result;
}
int main(){
   int N = 16;
   std::cout<< "Number of matches to find the winner are: " <<
   numOfMatches(N);
   return 0;
}

輸出

Number of matches to find the winner are: 15

雙淘汰賽

對於雙淘汰賽,勝者組的比賽場數與單淘汰賽相同。對於失敗者組,我們有 **(N – 2)** 場比賽。決賽中的比賽場數可以是 1 或 2,這取決於哪個組贏得了決賽。

Number of matches
   = number of matches for winner category
   + number of matches for loser category
   + number of matches in grand finale
   = (N – 1) + (N – 2) + 1 (or 2)

示例

#include <iostream>
using namespace std;
int main(){
   int N = 8;
   // Number of Matches for the winners’ category
   int result1 = (N - 1);
   // Number of Matches for the losers’ category
   int result2 = (N - 2);
   int num1 = result1 + result2 + 1;
   int num2 = result1 + result2 + 2;
   std::cout<< "Number of matches" << std::endl;
   std::cout<< "When winner is from winners' category are: " << num1<<
   std::endl;
   std::cout<< "When winner is from losers' category are: " << num2 <<
   std::endl;
   return 0;
}

輸出

Number of matches
When winner is from winners' category are: 14
When winner is from losers' category are: 15

迴圈賽

對於每個隊伍,將會有 **(N - 1)** 場比賽。此外,每個隊伍都需要與其他所有選手或隊伍競爭。因此,所需的比賽場數將為 **(N * (N - 1))**。但是,每場比賽都涉及兩個隊伍,上述公式是所需比賽場數的兩倍。

因此,確定勝者所需比賽場數的最終公式為:

(N * (N - 1)) / 2

示例

以下是一個計算確定勝者所需比賽場數的示例:

#include <iostream>
using namespace std;
int numOfMatches(int N){
   int result = (N * (N - 1)) / 2;
   return result;
}
int main(){
   int N = 7;
   std::cout<< "Number of matches to find the winner are: " << numOfMatches(N);
   return 0;
}

輸出

Number of matches to find the winner are: 21

結論

我們討論了在單淘汰賽、雙淘汰賽和迴圈賽中查詢確定勝者所需比賽場數的方法。我們對這些型別的錦標賽使用了不同的公式。

更新於:2024年1月5日

297 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

開始
廣告