UVALive 3882 And Then There Was One 约瑟夫环问题

约瑟夫环问题是一类经典问题,具体的问题描述如下

已知n个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。

抄自百度百科2333

模拟法不谈,数学上对于这类问题有一般的递推公式如下
F(i)=(F(i1)+m)modi(1<i<=n)
m为出去的人的编号
F(n)即为计数从0号作起点,出去n-1个人时留下的人的编号(0~n-1)

该题中套公式即可。最后答案加上m。

#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<stack>
#include<queue>
#include<fstream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<functional>
#include<cmath>
#define CLR(x) memset(x,0,sizeof(x))
#define SETMAX(x) memset(x,0x3f,sizeof(x))
#define SETNO(x) memset(x,-1,sizeof(x))
#define ll long long
#define eps 3e-12
#define pow2(x) ((x)*(x))
#define forto(i,n) for(int i=0;i<n;i++)
#define for1to(i,n) for(int i=1;i<=n;i++)
#define VI vector<int>
using namespace std;
const double PI=acos(-1.0);
#define INF 0x3f3f3f3f
#define NINF 0xbfffffff

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    int n,k,m;
    while(cin>>n>>k>>m,n||k||m)
    {
        int last=0;
        for(int i=2;i<n;i++)
        {
            last=(last+k)%i;
        }
        cout<<(last+m)%n+1<<endl;
    }
    return 0;
}
约瑟夫环问题是一道经典的数学问题,题目为:已知 n 个人(以编号 1,2,3 … n 分别表示)围成一圈,从第 k 个人开始报数,数到 m 的那个人出列,它的下一位又从 1 开始报数,数到 m 的那个人又出列,依次类推,直到所有人出列为止,由此产生一个出队编号的序列。 以下是使用 C 语言实现约瑟夫环问题的代码: ```c #include <stdio.h> int josephus(int n, int k, int m) { int i, s = 0; for (i = 2; i <= n; i++) s = (s + k) % i; s = (s + m) % n; return s + 1; } int main() { int n, k, m; printf("请输入 n, k, m 的值:"); scanf("%d%d%d", &n, &k, &m); printf("最后留下的人的编号是:%d\n", josephus(n, k, m)); return 0; } ``` 函数 `josephus` 的参数分别为 n,k,m,表示有 n 个人,从第 k 个人开始报数,数到 m 的那个人出列。函数的返回值为最后留下的人的编号。 在函数中,首先初始化变量 s 为 0,然后循环 n-1 次,每次计算出下一个出列的人的编号。具体来说,第 i 次循环时,假设前 i-1 个人已经出列,当前还剩下 n-i+1 个人,而上一次出列的人是编号为 s 的人,那么本次出列的人的编号为 (s+k) % i,其中 "%" 表示取模运算。最后剩下的那个人就是最后留下的人,其编号为 (s+m) % n。 在 `main` 函数中,从标准输入读入 n,k,m 的值,然后调用 `josephus` 函数计算最后留下的人的编号,并输出结果。 翻译如下: The Josephus problem is a classic mathematical problem. Given n people (represented by the numbers 1, 2, 3, ..., n) in a circle, starting from the kth person, count m people clockwise and remove the mth person. The next person starts counting from 1 again, and the process repeats until all people are removed, resulting in a sequence of removed numbers. The following is a C program to implement the Josephus problem: (略) The function `josephus` takes three parameters n, k, and m, representing the number of people, the starting point, and the count for removal, respectively. The function returns the number of the last person remaining. In the function, the variable s is initialized to 0, and the loop runs n-1 times, calculating the number of the next person to be removed each time. Specifically, in the i-th iteration, assuming the previous i-1 people have been removed, there are still n-i+1 people left, and the last removed person has the number s. The number of the current person to be removed is (s+k) % i, where "%" denotes the modulo operation. The last person remaining is the one with the number (s+m) % n. In the `main` function, the values of n, k, and m are read from standard input, and the `josephus` function is called to calculate the number of the last person remaining. The result is then output.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值