BUPT-homework/semester3/pset1/7-4-who-is-the-last-ARRAY.c

43 lines
638 B
C
Raw Normal View History

2022-11-20 21:22:03 +08:00
// 21:01 - 21:21
#include <stdio.h>
int step(int cur, int *dead, int N, int M) {
for (int i = 0; i < M; i++) {
cur = (cur + 1) % N;
while (dead[cur] == 1) {
cur = (cur + 1) % N;
}
}
return cur;
}
int main(void) {
int N, M;
scanf("%d,%d", &N, &M);
int deadCount = 0;
int dead[N];
for (int i = 0; i < N; i++) {
dead[i] = 0;
}
int cur = 0;
while (1) {
cur = step(cur, dead, N, M);
dead[cur] = 1;
deadCount++;
if (deadCount == N) {
printf("%d", cur + 1);
break;
} else {
printf("%d,", cur + 1);
}
cur = step(cur, dead, N, 1);
}
return 0;
}