SRM294Div2,250

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

You come across an apparently respectable man on the street who makes you a seemingly legitimate proposition: the opportunity to play a game of Three Card Monte. He has a table with three face-up cards in a row: two jokers and one ace, with the ace in the middle. After you make a small wager, he turns the cards face-down and begins to manipulate the cards, swapping cards two at a time. After he completes the swaps, you are then to guess which card is the ace.

The series of card swaps will be given to you in order as a String swaps, containing only the characters ‘L’, ‘R’, ‘E’, and ‘F’. swaps[0] indicates the first swap. The 4 characters indicate the following moves:

L: swap the left and middle cards
R: swap the right and middle cards
E: swap the cards on the ends (the left and right cards)
F: fake swap (no cards actually change position)
Write a method that returns the final position of the ace, after all the swaps have been performed. Your method should return “L” if the left card is the ace, “R” if the right card is the ace, and “M” if the ace is in the middle.

[java]
import java.util.Arrays;

public class ThreeCardMonte {

public String position(String swaps) {
int ace=0;
//System.out.println(swaps);
for (int i = 0; i < swaps.length(); i++) {
if (swaps.substring(i, i+1).equals("R")) {
//System.out.println("R:"+swaps.substring(i, i+1));
if (ace==0) {
ace=1;
}else if(ace==1){
ace=0;
}
}else if(swaps.substring(i, i+1).equals("L")) {
if (ace==0) {
ace=-1;
}else if(ace==-1){
ace=0;
}
}else if(swaps.substring(i, i+1).equals("E")) {
ace*=-1;
}
}
String ans="M";
if (ace==1) {
ans="R";
}else if(ace==-1){
ans="L";
}
return ans;
}
}

[/java]

コメント

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