Java/Java 알고리즘

백준 4500번 Bubble Gum JAVA 구현해보기

kimc 2022. 6. 6. 21:42

```

백준 4500번 Bubble Gum JAVA 구현해보기

```

이번 글을 통해 배워갈 내용

  1. 백준 4500번 풀이

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

 

4500번: “Bubble Gum, Bubble Gum, in the dish, how many pieces do you wish?”

The first value in the input file will be an integer t (0 < t < 1000) representing the number of test cases in the input file. Following this, on a case by case basis, will be a list of the names of the people (p), on a single line. Names will be no larger

www.acmicpc.net

 

백준 4500번 “Bubble Gum, Bubble Gum, in the dish, how many pieces do you wish? 는

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

 

사람들의 이름들이 주어지고

시작하는 사람

넘겨야 하는 횟수가

주어지고

주어진 사람의 이름순으로 왼쪽에서 오른쪽으로 순서가 순차적으로 바뀌고

(주어진 리스트의 끝일 경우 다음 순서는 처음)

마지막으로 주어진 순서가 우승자일 때

테스트 케이스만큼 반복해서

우승자들을 출력해주면 되는 문제입니다.


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

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


주어진 리스트를 돌면서

우승자를 출력했습니다.

(시작 인덱스 + 돌려야 하는 횟수 - 1) % 리스트 사이즈를 구하면

승자의 인덱스 값을 구할 수 있음을 활용했습니다.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

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 testCase = Integer.parseInt(br.readLine());

        for (int i = 0; i < testCase; i++) {
            List<String> inputList = Arrays
                    .stream(br.readLine().split(" "))
                    .collect(Collectors.toList());

            final int startIdx = inputList.indexOf(br.readLine());
            final int gumCount = Integer.parseInt(br.readLine());
            final int inputListSize = inputList.size();
            final int winnerIdx = (startIdx + gumCount - 1) % inputListSize;

            sb.append(inputList.get(winnerIdx)).append("\n");

        }

        sb.setLength(sb.length() - 1);
        System.out.print(sb);
    }
}

// https://codemasterkimc.tistory.com

 

 

읽어주셔서 감사합니다

 

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

 

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

 


 

728x90