곽로그
[백준1158, Java] 요세푸스 문제 본문
반응형
문제
코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine()," ");
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
Josephus josephus = new Josephus(N, K);
while(!josephus.circle.isEmpty()){
josephus.next();
}
ArrayList<Integer> jArray = josephus.getJArray();
bw.write("<");
for(int index = 0 ; index<jArray.size(); index++){
if(index==jArray.size()-1){
bw.write(String.valueOf(jArray.get(index)));
}
else{
bw.write(jArray.get(index)+", ");
}
}
bw.write(">");
bw.flush();
}
}
class Josephus{
LinkedList<Integer> circle;
ArrayList<Integer> jArray;
int N;
int K;
int pointer;
Josephus(int N, int K){
this.N = N;
this.K = K;
this.pointer = 0;
circle = new LinkedList<>();
for(int num = 1; num<=N ; num++) {
circle.add(num);
}
jArray = new ArrayList<>();
}
public void next(){
pointer += (K-1);
pointer = pointer%circle.size();
jArray.add(circle.remove(pointer));
}
public int getPointer(){
return pointer;
}
public ArrayList<Integer> getJArray(){
return jArray;
}
public LinkedList<Integer> getCircle(){
return circle;
}
}
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[백준 10820, Java] 문자열 분석 (0) | 2020.10.27 |
---|---|
[백준 1935, Java] 후위표기식2 (0) | 2020.10.26 |
백준 19236 청소년 상어 (Java) (0) | 2020.10.17 |
[백준 15686, Java] 치킨배달 (0) | 2020.10.15 |
[백준 14499, Java] 주사위 굴리기 (0) | 2020.10.15 |
Comments