UVa 130 - Roman Roulette

题目:约瑟夫环变形,每次第k个出去后,他后面的第k个人站到他的位置,然后从这个位置继续;

            现在你的编号,是1号,问从几号开始你回剩到最后。

分析:数论,模拟。直接模拟计算即可。

            设从x位置开始则,最后剩下的人是s(编号0~n-1)有,你的新编号为m-x+1 = s,则x = m-s+1。

说明:看错题了,输出从1开始数的答案,WA了2次。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

int p[101];

int find(int s, int k, int n)
{
	while (k) 
		if (p[s = (s+1)%n] != -1)
			k --;
	return s;
}

int main()
{
	int n,k,m;
	while (cin >> n >> k && n) {
		m = n;
		for (int i = 0 ; i < m ; ++ i)
			p[i] = i;
		int s = (k-1)%n;
		for (int i = 1 ; i < m ; ++ i) {
			p[s] = -1;
			swap(p[find(s, k, n)], p[s]);
			s = find(s, k, n);
		}
		printf("%d\n",(m-p[s])%m+1);
	}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值