您的位置:首页 > 其它

uva 305 - Joseph(暴力)

2014-08-23 23:47 369 查看
题目链接:uva 305 - Joseph

题目大意:给定n,表示2n个人进行约瑟夫环游戏,前n个人为好人,后n个人为坏人,要求一个步长k,保证n个坏人死前没有好人挂掉。

解题思路:枚举步长利用公式递推判断即可。
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>

using namespace std;

bool judge (int n, int k) {
int pos = 0;
for (int i = 2 * n; i > n; i--) {
pos = (pos - 1 + k) % i;
if (pos < n)
return false;
}
return true;
}

int solve (int n) {
for (int i = n+1; true; i++)
if (judge(n, i))
return i;
}

int main () {
int n, ans[15];
for (int i = 1; i <= 14; i++)
ans[i] = solve(i);
while (scanf("%d", &n) == 1 && n) {
printf("%d\n", ans
);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: