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

题目:

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

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

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

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

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

最后剩余的人是幸存者,请问这个人的标号是多少?
这道题的思路在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[n][1]+1);
	}
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值