您的位置:首页 > 其它

hdu 5643King's Game(约瑟夫游戏,递归)

2016-03-16 15:45 190 查看
题目:

为了铭记历史,国王准备在阅兵的间隙玩约瑟夫游戏。它召来了 n(1\le n\le 5000)n(1≤n≤5000) 个士兵,逆时针围成一个圈,依次标号 1, 2, 3 ... n1,2,3...n。

第一轮第一个人从 11 开始报数,报到 11 就停止且报到 11 的这个人出局。

第二轮从上一轮出局的人的下一个人开始从 11 报数,报到 22 就停止且报到 22 的这个人出局。

第三轮从上一轮出局的人的下一个人开始从 11 报数,报到 33 就停止且报到 33 的这个人出局。

第 n - 1n−1 轮从上一轮出局的人的下一个人开始从 11 报数,报到 n - 1n−1 就停止且报到 n - 1n−1 的这个人出局。

最后剩余的人是幸存者,请问这个人的标号是多少?

这道题的思路在BC官网上已经说得很详细了,这边只给出代码了:

值得注意的的是如果你要开5000*5000的数组的话会爆内存。 你可以用滚动数组,不过我懒得改了用的short int 过了。

/* ***********************************************
Author :yzkAccepted
Created Time :2016/3/16 14:48:22
TASK :ggfly.cpp
LANG :C++
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
#include <complex>
using namespace std;
#define maxn 5010
short int dp[maxn][maxn];
void init()
{
int i,j;
for(i=1;i<maxn;++i)
{
for(j=1;j<maxn;++j)
{
if(i==1)
dp[i][j]=0;
else
{
int v=j%i;
int t=dp[i-1][j+1];
t=(t+v)%i;
dp[i][j]=t;
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t;
scanf("%d",&t);
init();
while(t--)
{
int n;
scanf("%d",&n);
printf("%d\n",(int)dp
[1]+1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu