SRM295Div2,300

TopCoder
この記事は約5分で読めます。

A very exciting Paper-Rock-Scissors tournament has just begun! In qualifications, each player must face all his opponents in head to head games consisting of 5 throws. On each throw, each player will choose one of the following: Paper (‘P’), Rock (‘R’) or Scissors (‘S’). The winner of a throw is determined as follows:

Paper always wins against Rock

Rock always wins against Scissors

Scissors always win against Paper

In case both players make the same choice, it’s a tie and no one wins.

The winner of a game is the player who wins more throws. If both players win the same number of throws, the game ends in a tie (since these are qualifications, ties are allowed). To determine who passed the qualifications, a scoring system is implemented as follows:

For every game won, the player is awarded one point.

For every game tied, the player is awarded half of a point.

For every game lost, the player is awarded zero points.

Only a single player will pass the qualifications, namely the player with the most points. If several players have the same amount of points, the player which comes first in the input wins.

All players observe a simple strategy: each of them prepares a sequence of throws and plays the same sequence in every game. Write a program that will receive a String[] players representing the sequence of throws for all players and returns the 0-based index of the player who will pass the qualifications. Each element of players will contain exactly 5 characters. Character ‘P’ represents a Paper throw, ‘S’ represents a Scissors throw, and ‘R’ represents a Rock throw.

[java]
import java.util.Arrays;

public class PaperRockScisQuals {

public int whoPassed(String[] players) {
//System.out.println(Arrays.toString(players));
double[] point = new double[players.length];
//init
for (int i = 0; i < point.length; i++) {
point[i]=0;
}

//calc
for (int i = 0; i < players.length; i++) {
for (int j = i+1; j < players.length; j++) {
int ipoint=0;
int jpoint=0;
for (int k = 0; k < 5; k++) {
ipoint+=RSP(players[i].toCharArray()[k],players[j].toCharArray()[k]);
jpoint+=RSP(players[j].toCharArray()[k],players[i].toCharArray()[k]);
}
if (ipoint==jpoint) {
point[i]+=0.5;
point[j]+=0.5;
}else if(ipoint>jpoint){
point[i]+=1;
point[j]+=0;
}else{
point[i]+=0;
point[j]+=1;
}
}
}
//System.out.println(Arrays.toString(point));

double max=0;
int max_index=0;
for (int i = 0; i < point.length; i++) {
if (point[i]>max) {
max=point[i];
max_index=i;
}
}
return max_index;
}

public int RSP(char s1,char s2){
if (s1==’R’) {
if (s2==’S’) {
return 1;
}
}else if (s1==’S’) {
if(s2==’P’){
return 1;
}
}else {
if (s2==’R’) {
return 1;
}
}
return 0;
}
}

[/java]

コメント

タイトルとURLをコピーしました