Description N个小朋友手拉手站成一个圆圈,从第一个小朋友开始循环报数,报到M的那个小朋友退到圈外,然后他的下一位重新报“1”。这样继续下去,直到最后只剩下一个小朋友,他原来站在什么位置上呢? Input 仅一行,有两个数N和M,其中N表示小朋友的人数,M表示报到数。1<N<1000,M<=N Output 仅一个数,留下的小朋友的编号。 Sample Input 8 3 Sample Output 7
// This is AC Code
#include <iostream> #define SIZE 1001 using namespace std; struct node // 定义一个节点 { int value; node* next; } a[SIZE], *p; int total = 0; int main() { int n, m, i, j; cin >> n >> m; if (m == 1) { cout << n << endl; return 0; } for (i = 1; i < n; i++) { a[i].value = i; a[i].next = &a[i+1]; } a[n].value = n; a[n].next = &a[1]; i = 1; j = 1; while (true) { p = &a[i]; i = (p->next)->value; j++; if (j == m) { p->next = a[i].next; i = a[i].next->value; total++; /*********** 上面是删 除节点阶段 ***********/ j = 1; if (total == n) { cout << a[i].value << endl; return 0; } } } return 0; }