Two players are
holding a darts tournament consisting of a series of games. In each
game, the participants make several throws and earn points. The winner of an
individual game is the player who scores more points than the opponent. If both
players score the same number of points, the game is considered a draw.
The winner of
the tournament is the player who wins more games.
Write a program
that determines the winner of the tournament.
Input. The first line contains
one positive integer n (1 ≤ n ≤ 1000) – the number of games
in the tournament. Each of the following n lines
contains two non-negative integers – the number of points scored by the first
and second players, respectively. All values do not exceed 1000.
Output. Print:
·
the number 1, if the winner of the tournament is the first player;
·
the number 2, if the winner is the second player;
·
the number 0, if the tournament ends in a draw.
Sample
input 1 |
Sample output 1 |
3 3 1 1 0 1 2 |
1 |
|
|
Sample
input 2 |
Sample output 2 |
2 1 1 0 5 |
2 |
SOLUTION
loops
Let us declare two variables, a and b, representing the
number of games won by the first and second players, respectively. For each
game, increase a by one if the first player wins, and increase b
by one if the second player wins.
After all n games are completed, compare the values of a and
b and print the result.
Read the number of games n. Initialize the variables a and b
with zeros – they will store the number of wins for each player.
scanf("%d",&n);
a = b = 0;
Read the results of n games and update the values of a and b
according to the outcome of each game.
for(i = 0; i < n; i++)
{
scanf("%d %d",&x,&y);
if (x > y) a++;
if (x < y) b++;
}
After processing all the games, compare the number of wins of the players
and print the final result.
if (a > b) puts("1");
else
if (a < b) puts("2");
else
puts("0");