본문 바로가기
Algorithm/BaekJoon[Java]

백준 1158

by 씨니 2022. 3. 29.
728x90

▶ 1158문제 - 요세푸스 문제

-1168 문제와 같음. 1168은 시간초과걸려서 아직 해결하지 못함 ㅜ.

 

▶ 풀이방법

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Main {
	static int num, n;
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringBuilder sb = new StringBuilder();
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		num = Integer.parseInt(st.nextToken());
		n = Integer.parseInt(st.nextToken());
		
		ArrayList<Integer> list = new ArrayList<>();
		
		for(int i = 1 ; i <= num; i++) list.add(i);
		
		int count = n-1;
		
		while(true) {
			sb.append(list.get(count));
			list.remove(count);
			if(list.size() == 0) break;
			sb.append(", ");
			count += n-1;
			count %= list.size();
		}
		
		bw.write("<" + sb + ">");
		bw.flush();
		bw.close();
		br.close();
	}
}
728x90

'Algorithm > BaekJoon[Java]' 카테고리의 다른 글

백준 10430  (0) 2022.03.30
백준 1406  (0) 2022.03.30
백준 11655  (0) 2022.03.29
백준 2743  (0) 2022.03.29
백준 10820  (0) 2022.03.29