1215. Rock, Paper, or Scissors?

 

Rock, Paper, Scissors is a two player game, where each player simultaneously chooses one of the three items after counting to three. The game typically lasts a pre-determined number of rounds. The player who wins the most rounds wins the game. Given the number of rounds the players will compete, it is your job to determine which player wins after those rounds have been played.

The rules for what item wins are as follows:

·        Rock always beats Scissors (Rock crushes Scissors)

·        Scissors always beat Paper (Scissors cut Paper)

·        Paper always beats Rock (Paper covers Rock)

 

Input. The first value will be an integer t (0 < t < 1000) representing the number of test cases. Following this, on a case by case basis, will be an integer n (0 < n < 100) specifying the number of rounds of Rock, Paper, Scissors played. Next will be n lines, each with either a capital R, P or S, followed by a space, followed by a capital R, P or S, followed by a newline. The first letter is Player 1's choice; the second letter is Player 2's choice.

 

Output. For each test case, report the name of the player (Player 1 or Player 2) that wins the game, followed by a newline. If the game ends up in a tie, print TIE.

 

Sample input

Sample output

3

2

R P

S R

3

P P

R S

S R

1

P R

Player 2

TIE

Player 1

 

 

SOLUTION

simple mathematics

 

Algorithm analysis

For each test case, compute the number of times the first player wins and how many times the second player wins. Then compare these values.

 

Algorithm realization

Read the input data.

 

scanf("%d",&tests);

while(tests--)

{

  scanf("%d\n",&n);

 

The number of wins for the first player win1 and for the second player win2 is set to zero.

 

  win1 = win2 = 0;

  for(i = 0; i < n; i++)

  {

 

Read the result of one round of the game and give the victory to the first or to the second player. The result of the round cannot be a draw.

 

    scanf("%c %c\n",&a,&b);

    if ((a == 'R') && (b == 'S')) win1++;

    if ((a == 'S') && (b == 'R')) win2++;

    if ((a == 'S') && (b == 'P')) win1++;

    if ((a == 'P') && (b == 'S')) win2++;

    if ((a == 'P') && (b == 'R')) win1++;

    if ((a == 'R') && (b == 'P')) win2++;

  }

 

Print the answer.

 

  if (win1 > win2)

    printf("Player 1\n");

  else

  if (win1 < win2)

    printf("Player 2\n");

  else

    printf("TIE\n");

}

 

Java realization

 

import java.util.*;

//import java.io.*;

 

public class Main

{

  public static void main(String[] args) //throws IOException

  {

    Scanner con = new Scanner(System.in);

    //Scanner con = new Scanner(new FileReader ("1215.in"));

    int tests = con.nextInt();

    while(tests-- > 0)

    {

      int win1 = 0, win2 = 0;

      int n = con.nextInt();

      for(int i = 0; i < n; i++)

      {

        char a = con.next().charAt(0);

        char b = con.next().charAt(0);

        if ((a == 'R') && (b == 'S')) win1++;

        if ((a == 'S') && (b == 'R')) win2++;

        if ((a == 'S') && (b == 'P')) win1++;

        if ((a == 'P') && (b == 'S')) win2++;

        if ((a == 'P') && (b == 'R')) win1++;

        if ((a == 'R') && (b == 'P')) win2++;

      }

      if (win1 > win2)

        System.out.println("Player 1");

      else

      if (win1 < win2)

        System.out.println("Player 2");

      else

        System.out.println("TIE");

    }

    con.close();

  }

}