从1-N , n个数中循环报名, 报名为step ( step< N) 的倍数 退出,
求最后出队的 数。
int realCounting(int data[], int len, int step) {
int i = 0;int j = 0;
int max = len;
int num = 0;
while (1) {
if ((i + 1) % step != 0) {
data[j++] = data[num++];
}
else {
data[num++] = 0;
}
i++;
if (i >= max) {
if (num == 1)
return 0;
max += j; // j expresses the count of left number those % step == 0.
j = 0;
num = 0;
}
}
return 0;
}
void counting(int n, int step) {
int* data = new int[n]; //skip the first element
memset(data, 0, n * sizeof(int));
for (int i = 0; i < n; i++) {
data[i] = i + 1;
}
int err = realCounting(data, n, step);
cout << ", " << data[0] << "-" << data[1] << endl;
delete data;
data = NULL;
}