C++ code to find out who won an n-round game

Suppose, there is a two-player game that has n rounds. The scores of the rounds are given in an array 'scores' where each element is of the format {P1 Score, P2 Score}. The player with the higher score wins a round, and a player wins the game if they have won more rounds; otherwise, it is declared as a draw. So, given the scores, we have to find out who has won the game.

So, if the input is like n = 4, scores = {{4, 3}, {3, 2}, {5, 6}, {2, 5}}, then the output will be Draw.

Steps

To solve this, we will follow these steps −

res := 0
while n is non-zero, do:
   a := first value of scores[n]
   b := second value of scores[n]
   res := res + ((if a > b, then 1, otherwise (if a  0, then "P1", otherwise (if res 

Example

Let us see the following implementation to get better understanding − 

#include 
using namespace std;
#define N 100
string solve(int n, vector> scores) {
   int res = 0;
   while(n--){
      int a = scores[n].first;
      int b = scores[n].second;
      res += (a > b ? 1 : (a  0 ? "P1" : (res > scores = {{4, 3}, {3, 2}, {5, 6}, {2,5}};
   cout

Input

4, {{4, 3}, {3, 2}, {5, 6}, {2, 5}}

Output

Draw
Updated on: 2022-03-11T05:49:01+05:30

476 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements