18198번: Basketball One-on-One
The input consists of a single line with no more than 200 characters: the record of one game. The record consists of single letters (either A or B) alternating with single numbers (either 1 or 2), and includes no spaces or other extraneous characters. Each
www.acmicpc.net
듀스 주의
- 제목
Basketball One-on-One
- 조건
시간 제한 : 초
메모리 제한 : MB
- 문제
Alice and Barbara played some friendly games of one-on-one basketball after work, and you agreed to help them keep score. The rules of the game were simple:
- Each successful shot by a player earns them either one or two points;
- The first player to eleven points wins, with one exception;
- If the score is tied 10–0, the previous rule is replaced by a “win by 2” rule: the first player to lead the other by at least two points wins.
So for example, 11–7, 9–11, and 14–12 are possible final scores (but not 14–13).
Whenever Alice or Barbara scored points, you jotted down an A or B (indicating a score by Alice or by Barbara) followed by a 1 or 2 (the number of points scored). You have some records of the games Alice and Barbara played in this format, but do not remember who won each game. Can you reconstruct the winner from the game record?
- 입력
The input consists of a single line with no more than 200 characters: the record of one game. The record consists of single letters (either A or B) alternating with single numbers (either 1 or 2), and includes no spaces or other extraneous characters. Each record will be a correct scoring history of a single completed game, played under the rules described above.
- 출력
Print a single character, either A or B: the winner of the recorded game.
예제 입력1 | 예제 출력1 |
A2B1A2B2A1A2A2A2 | A |
예제 입력2 | 예제 출력2 |
A2B2A1B2A2B1A2B2A1B2A1A1B1A1A2 | A |
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String game = scanner.nextLine();
boolean duece = false;
int score_a = 0, score_b = 0;
for (int i = 0 ; i < game.length() ; i += 2){
if(game.charAt(i) == 'A'){
score_a += game.charAt(i + 1) - '0';
}
if(game.charAt(i) == 'B'){
score_b += game.charAt(i + 1) - '0';
}
if (score_a == 10 && score_b == 10){
duece = true;
}
if (duece && Math.abs(score_a - score_b) >= 2){
if (score_a >= score_b + 2) System.out.println('A');
if (score_b >= score_a + 2) System.out.println('B');
break;
}
if (!duece && (score_a >= 11 || score_b >= 11)){
if (score_a >= 11) System.out.println('A');
if (score_b >= 11) System.out.println('B');
break;
}
}
scanner.close();
}
}
'Problem Solving > BaekJoon' 카테고리의 다른 글
[BOJ/백준] 12919 - A와 B 2 (0) | 2023.07.19 |
---|---|
[BOJ/백준] 18290 - NM과 K(1) (0) | 2023.07.19 |
[BOJ/백준] 14426 - 접두사 찾기 (0) | 2023.07.19 |
[BOJ/백준] 28281 - 선물 (0) | 2023.07.19 |
[BOJ/백준] 15489 - 파스칼 삼각형 (0) | 2023.07.19 |