Java/Java 알고리즘

백준 18198번 Basketball One-on-One JAVA 구현해보기

kimc 2022. 8. 17. 22:51

```

백준 18198번 Basketball One-on-One JAVA 구현해보기

```

이번 글을 통해 배워갈 내용

  1. 백준 18198번 풀이

https://www.acmicpc.net/problem/18198

 

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

 

백준 18198번 Basketball One-on-One는

난이도 브론즈 등급의 문제로서

 

A와 B가 농구 경기를 하는데

한 가지 예외를 제외하고 11점 이상일 경우 더 높은 점수를 가진 사람이 승자입니다.

예외는 두 사람의 점수차가 2점 이상일 경우에만 승리자가 나온다는 것입니다.

점수는 1, 2점 받을 수 있을 때

경기 결과를 출력해주면 됩니다.

 


30분 정도 위에 링크를 방문하셔서 풀어보시고

안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.


점수를 받고 비교해서 출력하는 것도 하나의 방법이지만

결과가 무조건 나온다는 조건하에

마지막에서 두 번째 문자열을 받아서 출력하는 방법도 있습니다.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        // 입력
        final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        final String input = br.readLine();

        int scoreA = 0;
        int scoreB = 0;
        char winner = 'C';

        for (int i = 0; i < input.length(); i += 2) {

            final char player = input.charAt(i);
            final int currentScore = Integer.parseInt(String.valueOf(input.charAt(i + 1)));


            // 점수 획득
            if (player == 'A') {
                scoreA += currentScore;
            } else if (player == 'B') {
                scoreB += currentScore;
            } else {
                throw new RuntimeException();
            }

            // 점수 비교
            if (scoreA > 10 || scoreB > 10) {

                if (scoreA - 1 != scoreB && scoreB - 1 != scoreA) {
                    if (scoreA < scoreB) {
                        winner = 'B';
                        break;
                    } else if (scoreA > scoreB) {
                        winner = 'A';
                        break;
                    }
                }
            }

        }

        System.out.print(winner);

    }


}

// https://codemasterkimc.tistory.com

 

읽어주셔서 감사합니다

 

무엇인가 얻어가셨기를 바라며

 

오늘도 즐거운 코딩 하시길 바랍니다 ~ :)

 


 

728x90