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(">");
}
'알고리즘 문제' 카테고리의 다른 글
[C/C++ 백준 11050번] 이항 계수 1 (Bronze 1) (Class 2) (0) | 2020.07.29 |
---|---|
[C/C++ 백준 11650번] 좌표 정렬하기 (Silver 5) (Class 2) (0) | 2020.07.29 |
[C/C++ 백준 5556번] 타일 (Silver 1) (0) | 2020.07.27 |
[C/C++ 백준 5212번] 지구온난화 (Silver 2) (0) | 2020.07.26 |
[C/C++ 백준 1920번] 수찾기 (Silver 4) (Class 2) (0) | 2020.07.26 |