Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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 resExample
Let us see the following implementation to get better understanding −
#includeusing 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
Advertisements
