数组、单向链表、链表、向量实现Josephus

本文探讨了如何使用数组、单向链表、链表和向量解决著名的Josephus问题。详细阐述了每种数据结构在解决此问题时的思路和步骤,对比了它们之间的优缺点。
摘要由CSDN通过智能技术生成
#include <iostream>

using namespace std;

const int N=1e6+10;
int a[N];

void SolveJosephus(int n,int r,int k)
{
    for(int i=0;i<n;i++)    a[i]=i;

    int cur=r-1;
    while(n>1)
    {
        cur=(cur+k-1)%n;
        for(int i=cur;i<n-1;i++)    a[i]=a[i+1];
        n--;
    }

    cout<<a[0]+1<<endl;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,r,k;
        cin>>n>>r>>k;
        SolveJosephus(n,r,k);
    }
    return 0;
}
#include <iostream>
#include <cstdlib>

using namespace std;

typedef struct node
{
    int number;
    struct node *next;
}Node;

Node *CreateList(int n)
{
    Node *head=NULL,*tail;

    for(int i=1;i<=n;i++)
    {
        Node *p=(Node *)malloc(sizeof(Node));
        p->number=i;
        p->next=NULL;

        if(head==NULL)  head=p;
        else    tail->next=p;
        
        tail=p;
    }

    tail->next=head;
    tail=head;

    return head;
}

void SolveJosephus(Node *head,int n,int r,int k)
{
    int i=1;
    Node *p=head;

    while(i<r)
    {
        p=p->next;
        i++;
    }
    
    while(p->next!=p)
    {
        for(i=1;i<k-1;i++)    p=p->next;

        Node *temp=p->next;
        p->next=temp->next;
        p=p->next;

        free(temp);
    }
    cout<<p->number<<endl;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,r,k;
        cin>>n>>r>>k;
        if(k==1)
        {
            cout<<(r==1 ? n:r-1)<<endl;
            continue;
        }
        Node *head=CreateList(n);
        SolveJosephus(head,n,r,k);
    }
    return 0;
}
#include <iostream>
#include <list>

using namespace std;

void SolveJosephus(int n,int r,int k)
{
    list<int>a;
    for(int i=1;i<=n;i++)    a.push_back(i);

    list<int>::iterator it=a.begin();
    for(int i=1;i<r;i++)    it++;

    while(a.size()>1)
    {
        for(int i=1;i<k;i++)
        {
            if(it==a.end())    it=a.begin();
            it++;
        }
        if(it==a.end())    it=a.begin();
        it=a.erase(it);
        if(it==a.end()) it=a.begin();
    }
    cout<<*it<<endl;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,r,k;
        cin>>n>>r>>k;
        SolveJosephus(n,r,k);
    }
    return 0;
}
#include <iostream>
#include <vector>

using namespace std;

void SolveJosephus(int n,int r,int k)
{
    vector<int>a;
    for(int i=0;i<n;i++)    a.push_back(i+1);

    int cur=r-1;
    while(a.size())
    {
        cur=(cur+k-1)%a.size();
        a.erase(a.begin()+cur);
    }
    cout<<a[cur]<<endl;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,r,k;
        cin>>n>>r>>k;
        SolveJosephus(n,r,k);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值