백준 2346

hong030·2023년 3월 12일
0
  • 실버 3단계 문제

풀이)
단순히 index 변수를 두고 value 만큼 좌우로 이동시키면 된다
index가 0보다 크거나 작으면 다시 list의 사이즈 크기로 계산하면 된다.
풍선을 하나씩 터뜨릴 때마다 크기가 작아지므로 size -= 1을 한다.

내 코드)

import java.io.*;
import java.util.*;

public class Backjoon2346 {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        int N = Integer.parseInt(br.readLine());
        int index = 0;

        ArrayList<int[]> list = new ArrayList<>();
        StringTokenizer st = new StringTokenizer(br.readLine());

        for (int x = 0; x < N; x++) {
            list.add(new int[]{x+1,Integer.parseInt(st.nextToken())});
        }

        while (list.size()!=1) {
            int move = list.get(index)[1];
            int size = list.size() - 1;

            sb.append(list.get(index)[0]).append(" ");

            list.remove(index);

            if (move > 0) {
                move--;
            }

            index = (index + move) % size;

            if (index < 0) {
                index += size;
            }

        }

        sb.append(list.get(0)[0]);

        System.out.println(sb);

    }

}

profile
자바 주력, 프론트 공부 중인 초보 개발자. / https://github.com/hongjaewonP

0개의 댓글