A tournament features 128 teams, and each match is a knockout match.
Jignesh Kumar
Write a program to determine the winner of a tournament and calculate the total number of matches required to identify the winner?
using System; using System.Collections.Generic;class Tournament {static void Main(){int totalTeams = 128;List teams = new List();for (int i = 1; i <= totalTeams; i ){teams.Add("Team " i);}int round = 1;while (teams.Count > 1){Console.WriteLine($"Round {round}: {teams.Count} teams remaining");teams = KnockoutRound(teams);round ;}Console.WriteLine($"The winner is {teams[0]}!");}static List KnockoutRound(List teams){List winners = new List();for (int i = 0; i < teams.Count; i = 2){string winner = SimulateMatch(teams[i], teams[i 1]);winners.Add(winner);Console.WriteLine($"{teams[i]} vs {teams[i 1]} - Winner: {winner}");}return winners;}static string SimulateMatch(string team1, string team2){Random rand = new Random();return rand.Next(0, 2) == 0 ? team1 : team2;} }
using System;
class Tournament{ public static void Main() { int totalTeams = 128; // Initial number of teams int totalMatches = totalTeams - 1; // Number of matches = teams - 1
Console.WriteLine("Total matches played in the tournament: " + totalMatches); // Simulate the rounds int round = 1; int teamsInCurrentRound = totalTeams; while (teamsInCurrentRound > 1) { int matchesInCurrentRound = teamsInCurrentRound / 2; Console.WriteLine($"Round {round}: {matchesInCurrentRound} matches"); // Half of the teams are eliminated in each round teamsInCurrentRound /= 2; round++; }}
Console.WriteLine("Total matches played in the tournament: " + totalMatches);
// Simulate the rounds
int round = 1;
int teamsInCurrentRound = totalTeams;
while (teamsInCurrentRound > 1)
{
int matchesInCurrentRound = teamsInCurrentRound / 2;
Console.WriteLine($"Round {round}: {matchesInCurrentRound} matches");
// Half of the teams are eliminated in each round
teamsInCurrentRound /= 2;
round++;
}
Example Output:Total matches played in the tournament: 127Round 1: 64 matchesRound 2: 32 matchesRound 3: 16 matchesRound 4: 8 matchesRound 5: 4 matchesRound 6: 2 matchesRound 7: 1 matches