Write a program to determine the winner of a tournament and calculate the total number of matches required to identify the winner?
Interviews2 answers
A tournament features 128 teams, and each match is a knockout match.
2 Answers
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 roundsint 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 roundteamsInCurrentRound /= 2;round++;}}
}
Example Output:
Total matches played in the tournament: 127
Round 1: 64 matches
Round 2: 32 matches
Round 3: 16 matches
Round 4: 8 matches
Round 5: 4 matches
Round 6: 2 matches
Round 7: 1 matches
Kandregula Durga SaiPosted Apr 25, 2025, 4:59 AM EDT
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;
}
}
manali guruprasad kadamPosted Jan 17, 2025, 7:51 AM EST

