```
백준 4493번 가위 바위 보? JAVA 구현해보기
```

이번 글을 통해 배워갈 내용
- 백준 4493번 가위 바위 보 풀이
https://www.acmicpc.net/problem/4493
4493번: 가위 바위 보?
첫째 줄에는 테스트 케이스의 개수 t(0 < t < 1000)가 주어진다. 각 테스트 케이스의 첫째 줄에는 가위 바위 보를 한 횟수 n(0 < n < 100)이 주어진다. 다음 n개의 줄에는 R, P, S가 공백으로 구분되어 주어
www.acmicpc.net
백준 4493번 가위 바위 보는
난이도 브론즈 등급의 문제로서
테스트 케이스만큼
라운드를 입력받고
라운드만큼
가위바위보 결과를 받았을 때
각 테스트 케이스 별로 결과를 출력해주면 되는 문제입니다.
30분 정도 위에 링크를 방문하셔서 풀어보시고
안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.
Round 객체를 활용해서 정보를 옮겼습니다
플레이어 1 이 이기면 1 비기면 0 지면 -1을 기준으로 삼아서
findScore 메서드를 만들고
findResult 메서드에서 스트림으로 findScore를 호출해서 최종 값이 양수면 Player1이 최종적으로 이긴 것이고
0이면 비긴 것 음수면 진 것으로 정보를 추합 하였습니다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
final int testCases = Integer.parseInt(br.readLine());
for (int i = 0; i < testCases; i++) {
final int rounds = Integer.parseInt(br.readLine());
List<Round> roundList = new ArrayList<>();
for (int j = 0; j < rounds; j++) {
String input = br.readLine();
roundList.add(new Round(input.charAt(0), input.charAt(2)));
}
sb.append(findResult(roundList)).append("\n");
}
if (sb.length() > 0) {
sb.setLength(sb.length() - 1);
}
System.out.print(sb);
}
private static String findResult(List<Round> roundList) {
int totalScore = roundList.stream().mapToInt(Main::findScore).sum();
String result = "TIE";
if (totalScore > 0) {
result = "Player 1";
} else if (totalScore < 0) {
result = "Player 2";
}
return result;
}
private static int findScore(Round round) {
char p1 = round.getPlayer1Choice();
char p2 = round.getPlayer2Choice();
int retVal = 0;
if ((p1 == 'R' && p2 == 'S') ||
(p1 == 'S' && p2 == 'P') ||
(p1 == 'P' && p2 == 'R')
) {
retVal = 1;
} else if ((p1 == 'S' && p2 == 'R') ||
(p1 == 'P' && p2 == 'S') ||
(p1 == 'R' && p2 == 'P')
) {
retVal = -1;
}
return retVal;
}
private static class Round {
char Player1Choice;
char Player2Choice;
public Round(char player1Choice, char player2Choice) {
Player1Choice = player1Choice;
Player2Choice = player2Choice;
}
public char getPlayer1Choice() {
return Player1Choice;
}
public char getPlayer2Choice() {
return Player2Choice;
}
}
}
// https://codemasterkimc.tistory.com
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩 하시길 바랍니다 ~ :)
728x90
'Java > Java 알고리즘' 카테고리의 다른 글
| 백준 5355번 화성 수학 JAVA 구현해보기 (0) | 2022.06.06 |
|---|---|
| 백준 2204번 도비의 난독증 테스트 JAVA 구현해보기 (1) | 2022.06.06 |
| 백준 9612번 Maximum Word Frequency JAVA 구현해보기 (0) | 2022.06.05 |
| 백준 9506번 약수들의 합 JAVA 구현해보기 (0) | 2022.06.05 |
| 백준 9610번 사분면 JAVA 구현해보기 (0) | 2022.06.05 |