알고리즘 및 자료구조/문제
algospot 조세푸스(JOSEPHUS)
ktko
2016. 3. 28. 00:38
import java.util.ArrayList; import java.util.LinkedList; import java.util.Scanner; //JOSEPHUS //https://algospot.com/judge/problem/read/JOSEPHUS public class JOSEPHUS { static LinkedList<Integer> joList; static int testCase; static int[][] result; public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); // 문자 입력을 인자로 Scanner 생성 testCase = Integer.parseInt(scan.nextLine().trim()); // 키보드 문자 입력 result = new int[testCase][2]; // 테스트 케이스 정답을 저장하는 배열 for (int i = 0; i < testCase; i++) { int a = scan.nextInt(); // 사람 명 int kill = Integer.parseInt(scan.nextLine().trim()); // 순서 joList = new LinkedList<>(); for (int j = 1; j <= a; j++) { joList.add(j); } int point = 0; int killPoint = kill - 1; for (int j = 0; j < a - 2; j++) { if (joList.size() <= point) point %= joList.size(); joList.remove(point); point += killPoint; } result[i][0] = joList.get(0).intValue(); result[i][1] = joList.get(1).intValue(); } for (int k = 0; k < testCase; k++) System.out.println(result[k][0] + " " + result[k][1]); } }