문제 출처 : https://www.acmicpc.net/problem/18198
언어 : Kotlin
문제 설명 :
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–10, 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초 (추가 시간 없음)
- 메모리 제한 : 512MB
입출력 예 :
입력 | 출력 |
A2B1A2B2A1A2A2A2 | A |
A2B2A1B2A2B1A2B2A1B2A1A1B1A1A2 | A |
풀이 :
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter
fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
val bw = BufferedWriter(OutputStreamWriter(System.out))
val score = IntArray(2)
val input = readLine()
for (i in 1 until input.length step 2) {
when (input[i - 1]) {
'A' -> score[0] += input[i].digitToInt()
'B' -> score[1] += input[i].digitToInt()
}
}
bw.write(if (score[0] > score[1]) "A" else "B")
bw.flush()
bw.close()
}
'백준 > 문제' 카테고리의 다른 글
15819번: 너의 핸들은 (0) | 2024.05.29 |
---|---|
14175번: The Cow-Signal (0) | 2024.05.29 |
25205번: 경로당펑크 2077 (0) | 2024.05.28 |
24510번: 시간복잡도를 배운 도도 (0) | 2024.05.28 |
20154번: 이 구역의 승자는 누구야?! (0) | 2024.05.28 |
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!