其实就是约瑟夫环用但循环列表来实现
#include#include using namespace std; int N, K; // 1<=N<=1000 1<=K<=9 typedef struct Node { int data; struct Node *next; } Node, *LNode; void creat_list(LNode &H) { LNode p, q; q = H; int i = 0; int x; //尾插法 for (int i = 0; i < N; i++) { p = new Node; p->data = i+1; p->next = NULL; q->next = p; q = p; } p->next = H; } void print_list(LNode &H) { LNode p = H->next; while (p != H) { cout << p->data << " "; p = p->next; } } void win_put(LNode &H){ int count=1,key=0;LNode p=H->next;LNode q=H;//count报数计数器 while(N-key!=1){ if(count%K==0||count%10==K){ LNode del; del=p; p=p->next; q->next=p; free(del); key++;//人数计数器 } else{ p=p->next; q=q->next; } if(p==H){ p=H->next; q=H; } count++; } cout< data; } int main() { cin >> N >> K; LNode H; H = new Node; H->next = NULL; creat_list(H); //print_list(H); win_put(H); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)