[アルゴリズム]伯準-11816(ジョセフ問題0)/java

9256 ワード

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Test_11866 {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder("<");
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int n = Integer.parseInt(st.nextToken());
		int k = Integer.parseInt(st.nextToken());
		
		Queue<Integer> queue = new LinkedList<Integer>();
		for(int i = 1 ; i <= n ; i++) {
			queue.offer(i);
		}
		while(true) {
			for(int i = 0 ; i < k-1 ; i++) {
				queue.offer(queue.poll());
			}
			sb.append(queue.poll()).append(", ");
			if(queue.isEmpty()) {
				sb.replace(sb.lastIndexOf(","),sb.lastIndexOf(", ")+1,">");
				break;
			}
		}
		
		System.out.print(sb);
	}
}