循环链表的应用_约瑟夫环

标题:约瑟夫环
时 限:500 ms
内存限制:2000 K
总时限:3000 ms
描述:
约瑟夫环
编号为1,2,3,……,n的n个人按顺时针方向围坐一圈。任选一个正整数作为报数上限m,从第一个人开始按顺时针方向自1开始顺序报数,报到m时停止报数。报m的人出列,从他在顺时针方向上的下一个人开始重新从1报数,如此下去,直至所有人全部出列为止。设计程序输出出列顺序。
输入:
人数n 报数上限m
人员记录1 (格式为:姓名 学号 性别 年龄 班级 健康状况)
人员记录2
人员记录n
输出:
第1次报数出列的人员记录
第2次报数出列的人员记录
第n次报数出列的人员记录
#include <iostream>

using namespace std;

typedef struct _Node
{
    string name;
    string no;
    string gender;
    int age;
    string classno;
    string health;
    struct _Node *Next;
}Node;

int getinfo(Node *stu)
{
    cin>>stu->name;
    cin>>stu->no;
    cin>>stu->gender;
    cin>>stu->age;
    cin>>stu->classno;
    cin>>stu->health;
    return 0;
}

int printinfo(Node *stu)
{
    cout<<stu->name<<" "<<stu->no<<" "<<stu->gender<<" "<<stu->age<<" "<<stu->classno<<" "<<stu->health<<endl;
    return 0;
}

int main()
{
    int totalnum, upno;
    string _name;
    cin>>totalnum>>upno;
    //creat head node
    Node *head,*s,*p,*q;
    head=new Node;
    getinfo(head);
    p=head;
    p->Next=p;
    //insert other node
    for (int i=2;i<=totalnum;i++)
    {
        s=new Node;
        getinfo(s);
        s->Next=p->Next;
        p->Next=s;
        p=p->Next;
    }
    p=q=head;
    int currno=totalnum;
    while(currno!=1)
    {
        for(int j=1;j<upno;j++)
        {
            p=p->Next;//p direct the node to be deleted
        }
        printinfo(p);
        while(q->Next!=p)
        {
            q=q->Next;//q is the pre node of p
        }
        //delete p
        q->Next=p->Next;
        //store p
        s=p;
        //move p to the next position
        p=p->Next;
        delete(s);
        currno--;
    }
    printinfo(p);
    delete(p);
    //printinfo(head);
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值