user-0 提问: 约瑟夫环大数据要怎么做？

回复:
user: user-0
content: 引用 7 楼 JiangWenjie2014 的回复: #include <stdio.h> #include <stdlib.h>  #define N 200000 #define M 5   struct list {   int prev;   int next; };  int Round (struct list *p, int cur) {   int i, pr, ne;    for (i = 0; i < M - 1; ++i) {     cur = p[cur].next;   }    pr = p[cur].prev;   ne = p[cur].next;    p[pr].next = ne;   p[ne].prev = pr;    //printf ("del %d\n", cur);    return ne; }  int main (int argc, char *argv[]) {   int idx, cur;    struct list *p = malloc (N * sizeof (*p));    for (idx = 0; idx < N; ++idx) {     p[idx].prev = (idx + N - 1) % N;     p[idx].next = (idx + 1) % N;   }  //  for (idx = 0; idx < N; ++idx) { //    printf ("%d) prev %d, next %d\n", idx, p[idx].prev, p[idx].next); //  }      printf ("Start\n");   for (idx = 0, cur = 0; idx < N; ++idx) {     cur = Round (p, cur);   }   printf ("Finish\n");    free (p);    return 0; } 时间还是超了，如果N=200000，时间大于1s，要怎样优化，这正是我所疑惑的

user: user-1
content: #include <stdio.h> #include <stdlib.h>  #define N 200000 #define M 5   struct list {   int prev;   int next; };  int Round (struct list *p, int cur) {   int i, pr, ne;    for (i = 0; i < M - 1; ++i) {     cur = p[cur].next;   }    pr = p[cur].prev;   ne = p[cur].next;    p[pr].next = ne;   p[ne].prev = pr;    //printf ("del %d\n", cur);    return ne; }  int main (int argc, char *argv[]) {   int idx, cur;    struct list *p = malloc (N * sizeof (*p));    for (idx = 0; idx < N; ++idx) {     p[idx].prev = (idx + N - 1) % N;     p[idx].next = (idx + 1) % N;   }  //  for (idx = 0; idx < N; ++idx) { //    printf ("%d) prev %d, next %d\n", idx, p[idx].prev, p[idx].next); //  }      printf ("Start\n");   for (idx = 0, cur = 0; idx < N; ++idx) {     cur = Round (p, cur);   }   printf ("Finish\n");    free (p);    return 0; }

user: user-0
content: 网上的约瑟夫公式都只是求最后一个数字，并不能将每次淘汰的数字输出

user: user-0
content: 引用 3 楼 JiangWenjie2014 的回复: // [0,1,2,3,...,n-2,n-1]:m // Round 0: POS = (m-1)%n // Let k be the next position(m%n) of the deleted element, so remaining elements are //  k,k+1,...,n-2,n-1,0,1,2,...,k-2 //  0,1,2,...,n-3,n-2 << "each minus k, that is, m%n" // Round 1: POS = (m-1)%(n-1) // Let k be the next position(m%(n-1)) of the deleted element, so remaining elements are //  k,k+1,...,n-2,n-1,0,1,2,...,k-3 //  0,1,2,...,n-4,n-3 << "each minus k, that is, m%(n-1)" // // At round i, if element at position j is deleted, this element will be at // position (j+m)%(n-i) at round i-1. // // As is known to all, at round n-1, of course element at position 0 will be // deleted. So we got the formula // Position(n-1) = 0; // Position(i) = (Position(i+1)+m)%(n-i);  int Joseph(int n, int m) {   int pos;   for (int i = n-1; i >= 0; --i) {     if (i == n-1)       pos = 0;     else       pos = (pos+m)%(n-i);   }   return pos; } 谢谢，可是题目要的不仅仅只是最后幸存那个数字，而是要输出每一次淘汰的数字，要过大数据，请问怎么解决呢？

user: user-2
content: 3楼好解法

user: user-1
content: // [0,1,2,3,...,n-2,n-1]:m // Round 0: POS = (m-1)%n // Let k be the next position(m%n) of the deleted element, so remaining elements are //  k,k+1,...,n-2,n-1,0,1,2,...,k-2 //  0,1,2,...,n-3,n-2 << "each minus k, that is, m%n" // Round 1: POS = (m-1)%(n-1) // Let k be the next position(m%(n-1)) of the deleted element, so remaining elements are //  k,k+1,...,n-2,n-1,0,1,2,...,k-3 //  0,1,2,...,n-4,n-3 << "each minus k, that is, m%(n-1)" // // At round i, if element at position j is deleted, this element will be at // position (j+m)%(n-i) at round i-1. // // As is known to all, at round n-1, of course element at position 0 will be // deleted. So we got the formula // Position(n-1) = 0; // Position(i) = (Position(i+1)+m)%(n-i);  int Joseph(int n, int m) {   int pos;   for (int i = n-1; i >= 0; --i) {     if (i == n-1)       pos = 0;     else       pos = (pos+m)%(n-i);   }   return pos; }

user: user-3
content: 搜“约瑟夫 公式”

user: user-0
content: 每次踢出的数都要依次输出，不仅仅是输出最后一个数。这该怎么做