[Leetcode] An extremely simple question: the number of matches in the game

topic:

Give you an integer n, which represents the number of teams in the game. The competition follows a unique format:

If the current number of teams is even, then each team will be paired with another team. A total of n / 2 games are played, and n / 2 teams are generated to enter the next round.
If the current number of teams is an odd number, then a random bye will be made and one team will be promoted, and the remaining teams will be matched. A total of (n-1) / 2 games are played, and (n-1) / 2 + 1 teams are generated to enter the next round.
Returns the number of matches made in the game until the winning team is determined.

Example 1:

Input: n = 7
Output: 6
Explanation: Details of the game:
-Round 1: Number of teams = 7, number of matches = 3, 4 teams advance.
-Round 2: Number of teams = 4, number of matches = 2, 2 teams advance.
-Round 3: The number of teams = 2, the number of matches = 1, and a winning team will be determined.
Total number of matches = 3 + 2 + 1 = 6

Example 2:

Input: n = 14
Output: 13
Explanation: Details of the game:
-Round 1: Number of teams = 14, Number of matches = 7, 7 teams advance.
-Round 2: Number of teams = 7, number of matching = 3, 4 teams advance. 
-Round 3: Number of teams = 4, number of matches = 2, 2 teams advance.
-Round 4: The number of teams = 2, the number of matches = 1, and a winning team will be determined.
Total number of matches = 7 + 3 + 2 + 1 = 13

prompt:

1 <= n <= 200

Problem-solving ideas:

The writing is very complicated. After reading the question, I immediately thought that I would need to write a while, based on the odds and odds of n, but then I thought: n teams decide 1 team, n-1 teams need to be eliminated, n-1 matches are required , The code is return n-1;

Guess you like

Origin blog.csdn.net/hbuxiaoshe/article/details/115079247