用链表解决约瑟夫(Josephus)问题

来源:互联网 发布:电脑卸载不了软件 编辑:程序博客网 时间:2024/06/02 03:30
#include<iostream>using namespace std;struct student{int no;struct student *next;};int main(){    int n,m,s,i,reminder;student *first,*last,*p;    cin >> n >>m>>s;reminder=n;    first=last=new student;first->no=1;for(i=1;i<n;i++){p=new student;last->next=p;last=last->next;last->next=first;p->no=i+1;}for(i=1;i<s;i++){first=first->next;last=last->next;}while(reminder--){for(i=1;i<m;i++){            first=first->next;last=last->next;}last->next=first->next;cout << first->no << endl;delete first;first=last->next;}return 0;}


原创粉丝点击