Tennis Contest

A Tennis contest was scheduled in a college. N number of players registered to participate in this contest. (A player is the winner if he wins in all the rounds of the contest. Every round has elimination. Thus, one-half of the players are eliminated in each round. For example, If P number of players were in a specific round, then P/2 players win and move to the next round). The number of players N and the players details (ID and Name) are given as input. The program should print the names of the players who were defeated by the winner of the contest from final round to the first round.

Note : Total number of players N will always be a power of 2 and N >= 2.

Input/Output Format:
Input:

The first line contains N.
The next N lines contain the player’s ID and name separated by a space.
The remaining lines contain the match details in the format WinnerIDvsRunnerID.

Output:
Print the names of the players who were defeated by the winner from final round to the first round.

Example Input/Output 1:
Input:

4
101 Ram
102 Sri
103 Sheik
104 Vel
101vs102
103vs104
101vs103

Output:
Sheik
Sri

def main():
    n = int(input())  # Read the number of teams as input

    b = []  # List to store the team numbers
    a = []  # List to store the team names

    # Read team numbers and names and store them in lists b and a
    for i in range(n):
        num, name = input().split()
        b.append(int(num))
        a.append(name)

    c = []  # List to store the first team in each match
    d = []  # List to store the second team in each match

    # Read the matches and store the teams in lists c and d
    for i in range(n - 1):
        team1, vs, team2 = input().split()
        c.append(int(team1))
        d.append(int(team2))

    l = 0

    # Iterate in reverse order to find the winner of the final match (c[n-2])
    for i in range(n - 2, -1, -1):
        if c[i] == c[n - 2]:
            l = d[i]
            for j in range(n):
                if l == b[j]:
                    print(a[j])
                    break

if __name__ == "__main__":
    main()
#include <iostream>
#include <string>

int main() {
    int n;
    std::cin >> n;  // Read the number of teams as input

    int b[100];  // Array to store the team numbers
    char a[100][10];  // 2D array to store the team names

    // Read team numbers and names and store them in arrays b and a
    for (int i = 0; i < n; i++) {
        std::cin >> b[i] >> a[i];
    }

    int c[100], d[100];  // Arrays to store the first and second teams in each match

    // Read the matches and store the teams in arrays c and d
    for (int i = 0; i < n - 1; i++) {
        std::cin >> c[i];
        std::cin.ignore(); // Ignore the 'vs'
        std::cin >> d[i];
    }

    int l = 0;

    // Iterate in reverse order to find the winner of the final match (c[n-2])
    for (int i = n - 2; i >= 0; i--) {
        if (c[i] == c[n - 2]) {
            l = d[i];
            for (int j = 0; j < n; j++) {
                if (l == b[j]) {
                    std::cout << a[j] << std::endl;
                    break;
                }
            }
        }
    }

    return 0;
}
using System;

public class Program {
    public static void Main() {
        int n = int.Parse(Console.ReadLine());  // Read the number of teams as input

        int[] b = new int[100];  // Array to store the team numbers
        string[,] a = new string[100, 10];  // 2D array to store the team names

        // Read team numbers and names and store them in arrays b and a
        for (int i = 0; i < n; i++) {
            string[] input = Console.ReadLine().Split();
            b[i] = int.Parse(input[0]);
            a[i, 0] = input[1];
        }

        int[] c = new int[100];  // Array to store the first team in each match
        int[] d = new int[100];  // Array to store the second team in each match

        // Read the matches and store the teams in arrays c and d
        for (int i = 0; i < n - 1; i++) {
            string[] input = Console.ReadLine().Split(new char[] { 'v', 's' });
            c[i] = int.Parse(input[0]);
            d[i] = int.Parse(input[1]);
        }

        int l = 0;

        // Iterate in reverse order to find the winner of the final match (c[n-2])
        for (int i = n - 2; i >= 0; i--) {
            if (c[i] == c[n - 2]) {
                l = d[i];
                for (int j = 0; j < n; j++) {
                    if (l == b[j]) {
                        Console.WriteLine(a[j, 0]);
                        break;
                    }
                }
            }
        }
    }
}
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());  // Read the number of teams as input

        int[] b = new int[100];  // Array to store the team numbers
        String[][] a = new String[100][10];  // 2D array to store the team names

        // Read team numbers and names and store them in arrays b and a
        for (int i = 0; i < n; i++) {
            String[] input = scanner.nextLine().split(" ");
            b[i] = Integer.parseInt(input[0]);
            a[i][0] = input[1];
        }

        int[] c = new int[100];  // Array to store the first team in each match
        int[] d = new int[100];  // Array to store the second team in each match

        // Read the matches and store the teams in arrays c and d
        for (int i = 0; i < n - 1; i++) {
            String[] input = scanner.nextLine().split("vs");
            c[i] = Integer.parseInt(input[0].trim());
            d[i] = Integer.parseInt(input[1].trim());
        }

        int l = 0;

        // Iterate in reverse order to find the winner of the final match (c[n-2])
        for (int i = n - 2; i >= 0; i--) {
            if (c[i] == c[n - 2]) {
                l = d[i];
                for (int j = 0; j < n; j++) {
                    if (l == b[j]) {
                        System.out.println(a[j][0]);
                        break;
                    }
                }
            }
        }
    }
}
const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('Enter the number of teams: ', n => {
  const b = [];  // Array to store the team numbers
  const a = [];  // 2D array to store the team names

  // Read team numbers and names and store them in arrays b and a
  for (let i = 0; i < n; i++) {
    rl.question('Enter the team number and name separated by space: ', input => {
      const [num, name] = input.split(' ');
      b.push(parseInt(num));
      a.push([name]);
      if (b.length === n) {
        readMatches();
      }
    });
  }

  const c = [];  // Array to store the first team in each match
  const d = [];  // Array to store the second team in each match

  function readMatches() {
    // Read the matches and store the teams in arrays c and d
    for (let i = 0; i < n - 1; i++) {
      rl.question('Enter the match in the format "team1 vs team2": ', match => {
        const [team1, vs, team2] = match.split(/s+vss+/);
        c.push(parseInt(team1));
        d.push(parseInt(team2));
        if (c.length === n - 1) {
          findWinner();
        }
      });
    }
  }

  function findWinner() {
    let l = 0;

    // Iterate in reverse order to find the winner of the final match (c[n-2])
    for (let i = n - 2; i >= 0; i--) {
      if (c[i] === c[n - 2]) {
        l = d[i];
        for (let j = 0; j < n; j++) {
          if (l === b[j]) {
            console.log(a[j][0]);
            break;
          }
        }
      }
    }

    rl.close();
  }
});
#include <stdio.h>
#include <stdlib.h>

int main() {
    // Declare variables
    int n, i, j, k, l = 0, m, b[100], c[100], d[100];
    char a[100][10];

    // Read the number of teams as input
    scanf("%d", &n);

    // Read team numbers and names and store them in arrays b and a
    for (i = 0; i < n; i++) {
        scanf("%d%s", &b[i], a[i]);
    }

    // Read the matches and store the first and second teams in arrays c and d
    for (i = 0; i < n - 1; i++) {
        scanf("%dvs%d", &c[i], &d[i]);
    }

    // Find the winner of the final match (c[n-2]) and print their name
    for (i = n - 2; i >= 0; i--) {
        if (c[i] == c[n - 2]) {
            l = d[i];
            for (j = 0; j < n; j++) {
                if (l == b[j]) {
                    printf("%sn", a[j]);
                    break;
                }
            }
        }
    }

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

More posts. You may also be interested in.