约瑟夫环C++


class BoyNode
{
public:
    BoyNode(int no)
    {
        this->no = no;
    }
    void SetNo(int no)
    {
        this->no = no;
    }
    int GetNo()
    {
        return this->no;
    }
    void setNext(BoyNode *Node)
    {
        next = Node;
    }
    int no;
    BoyNode * next;
};
//构建一个单向循环链表思路:
//1.先创建一个节点,让first指向该节点,并形成一个环形
//2.后面每创建一个新节点,就把该节点,加入到已有的环形链表中即可
//遍历循环链表
//1.先设一个辅助指针curBoy,指向first节点
//2.然后通过一个while循环遍历该环形链表即可  curBoy->next==first  结束
class CricleList
{
public:
    void addBoy(int nums)
    {
        if (nums < 1)
        {
            cout << "nums is wrong!" << endl;
            return;
        }
        BoyNode *curBoy = nullptr;
        for (int i = 1; i <= nums; i++)
        {
            BoyNode *Boy = new BoyNode(i);
            if (i == 1)
            {
                first = Boy;
                first->setNext(first);
                curBoy = first;
            }
            else
            {
                curBoy->setNext(Boy);
                Boy->setNext(first);
                curBoy = Boy;
            }
        }
    }

    void showBoy()
    {
        if (first == nullptr)
        {
            cout << "没有任何小孩" << endl;
            return;
        }
        BoyNode *curBoy = first;
        while (true)
        {
            cout << "小孩的编号:" << curBoy->GetNo()<<endl;
            if (curBoy->next == first)
            {
                break;
            }
            curBoy = curBoy->next;
        }
    }
    //约瑟夫出圈
    //1.创建一个辅助指针helper,事先应该指向环形链表的最后节点
    //2.让first和helper移动到要报数的小孩那
    //3.当小孩报数时,让first和helper指针同时移动m-1次
    //4.这时first指向小孩节点出圈
    //first=first->next;
    //helper->next=first;
    //startNo:从第几个人开始报数
    //countNo:报几个数出圈
    //nums:圈内有几个小孩
    void countBoy(int startNo,int countNo,int nums)
    {
        if (startNo<1||startNo>nums)
        {
            cout << "该约瑟夫环问题不对" << endl;
            return;
        }
        if (first == nullptr)
        {
            cout << "没有小孩" << endl;
        }
        BoyNode *helper = first;
        while (true)
        {
            if (helper->next == first)
            {
                break;
            }
            helper = helper->next;
        }
        for (int i = 0; i < startNo-1; i++)
        {
            first = first->next;
            helper = helper->next;
        }
        while (true)
        {
            if (helper == first)
            {
                break;
            }
            for (int i = 0; i < countNo - 1; i++)
            {
                first = first->next;
                helper = helper->next;
            }
            cout << "小孩编号为:" << first->GetNo() << endl;
            first = first->next;
            helper->next = first;
        }
        cout << "小孩编号为:" << first->GetNo() << endl;
    }

private:
    BoyNode *first = nullptr;
};


int main()
{
    CricleList List;
    List.addBoy(5);
    List.showBoy();
    cout << endl;
    cout << "约瑟夫环:" << endl;
    List.countBoy(1, 2, 5);
    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值