2 20 40
1 7 19 1 19 37
// 注意点为 是每轮删去数后才算 N 值是否小于 3. 即是以 2 为跳数, 删去数列时, 就是只剩下 3 个数时也要删。
代码:
#include <cstdio> using namespace std; typedef struct node Node; const int all = 3; struct node { int x; Node *next; }; int make( Node head, int n ); Node* New( int n ); int main(void) { int t, num; Node Head, *tmp; scanf( "%d", &t ); while( t -- ){ scanf( "%d", &num ); Head.next = New( num ); num = make( Head, num ); while( num -- ){ printf( "%d", Head.next->x ); tmp = Head.next->next; delete Head.next; Head.next = tmp; if( num ){ putchar( ‘ ‘ ); } } putchar( ‘\n‘ ); } return 0; } Node* New( int n ) { Node head, *tmp;; head.next = NULL; if( n ){ head.next = tmp = new Node; tmp->x = 1; tmp->next = NULL; for( int i=2; i <= n; ++ i ){ tmp->next = new Node; tmp = tmp->next; tmp->x = i; tmp->next = NULL; } } return head.next; } int make( Node head, int n ) { Node *tmp, *tmp2; while( n > all ){ tmp = head.next; while( tmp ){ if( tmp && tmp->next ){ tmp2 = tmp->next->next; delete tmp->next; tmp->next = tmp2; tmp = tmp2; -- n; } else break; } tmp = head.next; while( n > 3 ){ if( tmp && tmp->next && tmp->next->next ){ tmp2 = tmp->next->next->next; delete tmp->next->next; tmp->next->next = tmp2; tmp = tmp2; -- n; } else break; } } return n; }
原文:http://www.cnblogs.com/seana/p/5232584.html