标签:blog os io div amp log new ios
#include <iostream> using namespace std; struct Node { Node *next; int data; }; void createCircle(Node* &L,int n,int k) { int i = 0; Node*p = L; p ->data = k; while(i<n-1) { Node *q = new Node; i++; if(i+k == n) q->data = n; else q ->data = (i+k)%n; p ->next = q; p = p->next; } p->next = L; } void JosephusProblem(Node* &L,int n,int k,int m) { cout<<"The sequence of dequeue queue is:"<<endl; createCircle(L, n, k); Node *p = L; while (n--) { int count = 1; while (count<m-1) { p = p->next; count++; } Node* q= p->next; cout<<q->data<<" "; p->next = q->next; p = p->next; free(q); } cout<<endl; cout<<"Winner: "<<p->data<<endl; } int main() { Node* L = new Node; JosephusProblem(L,6,1,3); return 0; }
标签:blog os io div amp log new ios
原文地址:http://www.cnblogs.com/cliviazhou/p/3911294.html