约瑟夫问题

Problem Description

n个人想玩残酷的死亡游戏,游戏规则如下:

n个人进行编号,分别从1到n,排成一个圈,顺时针从1开始数到m,数到m的人被杀,剩下的人继续游戏,活到最后的一个人是胜利者。

请输出最后一个人的编号。

Input

输入n和m值。

Output

输出胜利者的编号。

Sample Input

5 3

Sample Output

4

Hint

第一轮:3被杀第二轮:1被杀第三轮:5被杀第四轮:2被杀

方法一:
#include<bits/stdc++.h>
using namespace std;
struct node
{
    int data;
    struct node *next;
};

void ff(struct node *head, int m, int n)
{
    int num = 0;
    struct node *p, *q;
    q = head;
    while(q->next != head)
        q = q->next;
    while(q->next != q)
    {
        p = q->next;
        num++;
        if(num % n == 0)
        {
            q->next = p->next;
            free(p);
        }
        else q = p;
    }
    printf("%d\n", q->data);
}

int main()
{
    int n, i, m;
    scanf("%d%d", &m, &n);
    struct node *p,*tail,*head;
    p=(struct node*)malloc(sizeof(struct node));
    p->data=1;
    p->next=NULL;
    head=p;
    tail=p;
    for(i=2; i<=m; i++)
    {
        p=(struct node*)malloc(sizeof(struct node));
        p->data=i;
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    tail->next=head;
    ff(head, m, n);
    return 0;
}
方法二:
#include<bits/stdc++.h>
using namespace std;
struct node
{
    int data;
    struct node *next;
};
void f(struct node *head,int m,int n)
{
    struct node*p,*q;
    int num=0;
     q=head;
    while(q->next != head)
        q = q->next;
    while(q->next !=q)
    {
        p = q->next;
        num++;
        if(num % n == 0)
        {
            q->next = p->next;
            free(p);
        }
        else q = p;
    }
    printf("%d\n", q->data);



}
int main()
{
    int n,m;
    struct node*p,*q,*k;
    struct node *head;
    p=(struct node*)malloc(sizeof(struct node));
    p->next=NULL;
    p->data=1;
    q=p;
    scanf("%d %d",&m,&n);
    for(int i=2; i<=m; i++)
    {
        k=(struct node*)malloc(sizeof(struct node));
        k->data=i;
        k->next=NULL;
        q->next=k;
        q=k;

    }
    q->next=p;
    f(p,m,n);
    return 0;;

}

方法三:
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};
void f(struct node*head,int n, int k)
{
    int num=0;
    struct node *p,*q;
    q=head;
    p=head;
    while(q->next!=head)
        q=q->next;
    while(q->next!=q)
    {
        num++;
        if(num%k==0)
        {
            q->next=p->next;
            free(p) ;
            p=q->next;
        }
        else
        {
            p=p->next;
            q=q->next;

        }
    }
    printf("%d\n",q->data);
}
int main()
{

    int n,k;
    struct node *head,*q,*p;
    p=(struct node*)malloc(sizeof(struct node));
    p->data=1;
    p->next=NULL;
    q=p;
    head=p;
    scanf("%d %d",&n,&k);
    for(int i=2;i<=n;i++)
    {
        p=(struct node*)malloc(sizeof(struct node));
        p->data=i;
        p->next=NULL;
        q->next=p;
        q=p;
    }
    q->next=head;
    f(head,n,k);
    return 0;
}




    原文作者:约瑟夫环问题
    原文地址: https://blog.csdn.net/weixin_43892738/article/details/88703343
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞