본문 바로가기

알고리즘 문제

[C/C++ 백준 11866번] 요세푸스 문제 0 (Silver 4) (Class 2)

https://www.acmicpc.net/problem/11866

 

11866번: 요세푸스 문제 0

첫째 줄에 N과 K가 빈 칸을 사이에 두고 순서대로 주어진다. (1 ≤ K ≤ N ≤ 1,000)

www.acmicpc.net

카운터를 두개 만들어서, 같이 돌리면서 찾아주자. 다행히 범위가 크지 않아 시간 초과를 걱정할 일은 없었다.

코드는 다음과 같다.

 

#include <cstdio>
#include <cstring>
using namespace std;
int main(void){
	int People[1001]={}, cnt=0, N, K, delcnt=0, Kcnt=0;
	scanf("%d %d",&N,&K);
	memset(People, 0, 1001*sizeof(int));
	People[0] = -1;
	printf("<");
	while(cnt<N){
		delcnt++;
		if(delcnt>N)
			delcnt -= N;
		if(People[delcnt]==0){
			Kcnt++;
			if(Kcnt==K){
				if(cnt==N-1){
					printf("%d",delcnt);
					cnt++;
				}	
				else{
					People[delcnt] = -1;
					printf("%d, ",delcnt);
					Kcnt -= K;
					cnt++;
				}	
			}
		}
	}
	printf(">");
}