๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’ป Computer Science/- Data Structure, Algorithm

[์•Œ๊ณ ๋ฆฌ์ฆ˜ PS] ๋ฐฑ์ค€ 11866 ์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ 0 ์ž๋ฐ” ๋ฌธ์ œํ’€์ด

by Wonit 2021. 1. 18.

๋ฌธ์ œ

ํ•ด๋‹น ํฌ์ŠคํŒ…์€ ๋ฐฑ์ค€์˜ 11866๋ฒˆ ์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ ์˜ ์ ‘๊ทผ๊ณผ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.
์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ & ์ถœ๋ ฅ

ํ•ด๊ฒฐ๋ฒ•

์ด ๋ฌธ์ œ๋Š” Queue ์ปฌ๋ ‰์…˜์„ ์›ํ˜• ํ์ฒ˜๋Ÿผ ๋งŒ๋“ค๋ฉด ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

์ ‘๊ทผ๋ฒ•

  • n๊ฐœ์˜ ์ž…๋ ฅ์ด ๋“ค์–ด์˜ค๋ฉด 1 ~ n ๊นŒ์ง€ queue์— pushํ•ด์ค€๋‹ค.
  • queue์˜ size ๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉฐ k๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ dequeueํ•œ๋‹ค.
  • ๋งŒ์•ฝ k๋ฒˆ์งธ ์ธ๋ฑ์Šค๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ ๋‹ค์‹œ queue์— enqueueํ•ด ์›ํ˜• ํ๋กœ ๋งŒ๋“ ๋‹ค.

์ •๋‹ต ์ฝ”๋“œ

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int n = input.nextInt();
        int k = input.nextInt();
        Queue<Integer> queue = new LinkedList<>();

        for (int i = 0; i < n; i++) {
            queue.add(i+1);
        }
        System.out.print("<");
        while(n-- > 0) {
            for (int i = 0; i < k; i++) {
                if(i == k-1) {
                    if(n == 0) System.out.print(queue.poll());
                    else System.out.print(queue.poll() + ", ");
                }else {
                    queue.add(queue.poll());
                }
            }
        }
        System.out.print(">");
    }
}

์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋Œ“๊ธ€