[백준] 11866번 요세푸스 문제0 (= josephus problem0) - 재우스 프로그래밍 (C 언어)
2021. 2. 5. 09:07ㆍ백준
문제 링크 :www.acmicpc.net/problem/11866
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define DEBUG 0
#define MAX_QUEUE_SIZE 1001
typedef struct _queue {
int mem[MAX_QUEUE_SIZE];
int Front;
int Back;
} queue;
bool empty(queue *);
void pop(queue *);
void push(queue *, int);
size_t size(queue *);
int front(queue *);
int back(queue *);
void initQueue(queue *);
#if DEBUG
void printQueue(queue *, int);
#endif
int main(void)
{
int N = 0, K = 0; //사람의 수 N, K번째 사람 제거
int temp = 0;
queue *qu = (queue *)malloc(sizeof(queue));
initQueue(qu);
scanf("%d %d", &N, &K);
for (int i = 0; i < N; i++) {
push(qu, i + 1);
}
#if DEBUG
printQueue(qu, N);
#endif
printf("<");
for (int i = 1; i <= N; i++) {
if (i != 1) {
printf(", ");
}
for (int j = 0; j < K - 1; j++) {
temp = front(qu);
pop(qu);
push(qu, temp);
#if DEBUG
printQueue(qu, N);
#endif
}
temp = front(qu);
pop(qu);
printf("%d", temp);
#if DEBUG
printQueue(qu, N);
#endif
}
printf(">\n");
free(qu);
return 0;
}
bool empty(queue *qu)
{
if (qu->Front == qu->Back) { return true; }
else { return false; }
}
void pop(queue *qu)
{
qu->Front = (qu->Front + 1) % MAX_QUEUE_SIZE;
qu->mem[qu->Front] = 0;
return;
}
void push(queue *qu, int data)
{
qu->Back = (qu->Back + 1) % MAX_QUEUE_SIZE;
qu->mem[qu->Back] = data;
return;
}
size_t size(queue *qu)
{
if (qu->Back >= qu->Front)
return qu->Back - qu->Front;
else
return (qu->Back - qu->Front) + MAX_QUEUE_SIZE;
}
int front(queue *qu) { return qu->mem[(qu->Front + 1) % MAX_QUEUE_SIZE]; }
int back(queue *qu) { return qu->mem[qu->Back]; }
void initQueue(queue *qu)
{
for (int i = 0; i < MAX_QUEUE_SIZE; i++) {
qu->mem[i] = 0;
}
qu->Front = MAX_QUEUE_SIZE - 1;
qu->Back = MAX_QUEUE_SIZE - 1;
return;
}
#if DEBUG
void printQueue(queue *qu, int N)
{
printf("\nprintQueue\n");
for (int i = 0; i < 2 * N; i++) {
printf("%d ", qu->mem[i]);
}
printf("\nfront(%d): %d, back(%d): %d\n", qu->Front, front(qu), qu->Back, back(qu));
return;
}
#endif
'백준' 카테고리의 다른 글
[백준] 1976번 여행 가자 (= lets go travel) - 재우스 프로그래밍 (C 언어) (0) | 2021.02.07 |
---|---|
[백준] 2468번 안전 영역 (= safe area) - 재우스 프로그래밍 (C 언어) (0) | 2021.02.06 |
[백준] 2164번 카드2 (= card2) - 재우스 프로그래밍 (C 언어) (0) | 2021.02.04 |
[백준] 5639번 이진 검색 트리 (= binary search tree) - 재우스 프로그래밍 (C 언어) (0) | 2021.02.03 |
[백준] 2156번 포도주 시식 (= wine tasting) - 재우스 프로그래밍 (C 언어) (0) | 2021.02.03 |