沃瑟夫问题:约瑟夫问题的变体
#include <bits/stdc++.h>
using namespace std;
const int size = 5001;
int n, front = 1, rear = size;
int q[size + 1];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
rear = rear % size + 1;
q[rear] = i;
}
int x = 0, i = 1;
while (rear % size + 1 != front) {
int y = q[front];
front = front % size + 1;
x++;
if (x == i * i) {
printf("%d ", y);
x = 0;
i++;
} else {
rear = rear % size + 1;
q[rear] = y;
}
}
return 0;
}
因为n 最大是5000,一个for,一个while会超时间复杂度(1s)
希望帮助改良代码