剑指offer 编程题(45):圆圈中最后剩下的数(约瑟夫环)

本文探讨了经典的约瑟夫环问题,通过三种不同的方法实现了解决方案:使用链表模拟整个过程、采用循环链表简化操作以及借助向量进行高效处理。每种方法都详细解释了其实现思路及步骤,帮助读者理解约瑟夫环问题的核心算法。

每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0…m-1报数….这样下去….直到剩下最后一个小朋友,可以不用表演,并且拿到牛客名贵的“名侦探柯南”典藏版(名额有限哦!!^_^)。请你试着想下,哪个小朋友会得到这份礼品呢?(注:小朋友的编号是从0到n-1)

class Solution {
public:
    struct node
    {
        int no;
        node *next;
    };
    int LastRemaining_Solution(int n, int m)
    {
        node *head=new node;
        head->no=n-1;
        head->next=NULL;
        node *tail=head;//保留最后一个位置的节点一边将链表头尾连接
        for(int i=n-2; i>=0; i--)
        {
            node *p=new node;
            p->no=i;
            p->next=head;
            head=p;
        }
        tail->next=head;
        node *pre=tail;//保存被选中的节点前一个以便删去节点后重新连接链表
        while(head->next!=head)//判断条件是链表中只剩一个节点才停下
        {
            node *tmp=head;
            for(int i=0; i<m-1; i++)
            {
                tmp=tmp->next;
                pre=pre->next;
            }
            pre->next=tmp->next;
            head=pre->next;
            delete tmp;
        }
        return pre->no;
    }
};
class Solution {
public:
    struct node
    {
        int no;
        node *next;
    };
    int LastRemaining_Solution(int n, int m)
    {
        if(m <= 0 || n <= 0)
        {
            return -1;
        }
        node *head=new node;
        head->no = 0;
        node *pre = head;
        node *temp = NULL;
       for(int i = 1; i < n; i++)
       {
            temp = new node;
           temp->no = i;
            pre->next = temp;
            pre = temp;
        }
        temp->next = head;
        node *temp2 = NULL;
        while(n != 1)
        {
            temp2 = head;
            for(int i = 1;i < m-1;i++)
            {
                temp2 =temp2->next;
            }
            temp2->next = temp2->next->next;
            head =temp2->next;
            n--;
        }
        return head->no;
    }
};
class Solution {
public:
    int LastRemaining_Solution(int n, int m)
    {
        if(m==0||n==0)
            return -1;
        vector<int> a;
        for(int i=0;i<n;i++)
            a.push_back(i);
        int k=0;
        while(a.size()>1)
        {
            k=(k+m-1)%a.size();
            a.erase(a.begin()+k);
        }
        return a[0];
    }
};

问题描述:
已知n个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围。从编号1开始报数,数到k的那个人出列;他的下一个人又从1开始报数,数到k的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。
例如n=10,k=3时,输出的出列顺序是3,6,9,2,7,1,8,5,10,4。

#include<iostream>
#include<vector>
using namespace std;

int main()
{
    vector<int> a;              //定义向量对象a
    int n,k,x=0;
    cout<<"请输入总人数n和出环位置k:"<<endl;
    cin>>n>>k;
    for(int i=0; i<n; i++)
    {
        a.push_back(i+1);   //初始化向量对象a,依次将i+1插入尾部
    }
    while(a.size())             //当向量中元素个数不为0
    {
        x = (x+k-1) % a.size();     //计算出环位置
        cout<<a[x]<<" ";
        a.erase(a.begin()+x);       //在向量中删除此元素
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值