반응형

1158번

Queue로 구현한경우

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;

public class Main {

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

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		Queue<Integer> queue = new LinkedList<Integer>();

		String[] inputs = br.readLine().split(" ");
		int N = Integer.parseInt(inputs[0]);
		int K = Integer.parseInt(inputs[1]);

		int locate = 1;

		for (int i = 1; i <= N; i++) {
			queue.add(i);
		}

		bw.write("<");
		while (queue.size() != 1) {
			if (locate % K == 0) {
				bw.write(queue.poll() + ", ");
			} else {
				queue.add(queue.poll());
			}
			locate++;
		}
		bw.write(queue.poll() + ">");

		bw.flush();
		bw.close();
	}

}
반응형

'알고리즘 > 백준 문제[추후 옮길예정]' 카테고리의 다른 글

[JAVA] 백준 10799번  (0) 2021.05.08
[JAVA] 백준 17413번  (0) 2021.05.07
[JAVA] 백준 10845번  (0) 2021.05.07
[JAVA] 백준 1406번  (0) 2021.05.07
[JAVA] 백준 9093번  (0) 2021.05.06

+ Recent posts