package test5;
import java.util.LinkedList;
import java.util.Queue;
public class test {
//내부 클래스
static class person{
int num;//사람 번호
int cnt;//큐에서 나왔을 때 몇개를 줄 지
person(int num, int cnt){
this.num=num;
this.cnt=cnt;
}
}
public static void main(String[] args) {
int N=20;//마이쮸 개수
int pNum=1; //현재 사람 번호
Queue<person> q=new LinkedList<person>();
//제일 처음에 1번 사람을 넣고 시작, 1번 사람은 제일 처음에 나올 때 마이쮸 1개 가진다.
q.offer(new person(pNum, 1));
while(N>0) {//마이쮸가 남아있을 때가지 무한루프
person p=q.poll();//큐에서 첫번재 사람을 꺼낸다.
System.out.print(p.num+" ");
System.out.println(p.cnt);
N-=p.cnt; //마이쮸 주기
//1. N>0
//2. N==0//마이쮸 소진
//3. N<0//마이쮸 소진
//->소진시킨 사람이 마지막 마이쮸를 가져간 사람이므로 p.num;
if (N<=0) {
System.out.println(p.num);
}else {//소진되지 않은 상황
p.cnt++;
q.offer(p);
q.offer(new person(++pNum,1));
}
}
}
}