UVA 305 Joseph (约瑟夫环 打表)
Joseph |
The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, ..., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.
Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.
Input
The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14.
Output
The output file will consist of separate lines containing m corresponding to k in the input file.
Sample Input
3
4
0
Sample Output
5
30
假设有2k个人围着一个圆桌坐着,前k个是好人,后k个是坏人 。现在开始,每m个人踢掉一个,比如有6个人,m=5,那么,被踢掉的人依次是5,4,6,2,3,1。现在要求,在踢掉第一个好人前,必需把所有的坏人踢掉,问,给定一个k,求满足这个要求的最小的m。
思路:
直接模拟会超时。。约瑟夫环的问题可以推出数学的公式。。再由公式去模拟
当前位置 = (步数 + 当前位置) % 人数 (注意。。如果为0.说明是到了最后一个位置)
因为好人为前K个。 所以如果当前位置在区间[1, K]就不满足。
每次找完一个人之后。把这个人除去。所以位置要前移一位。。
然后人数减一。。如果人数减少到k个了。就表明这次是成立的
这样进行模拟就可以大大减少时间。。。
但是。。但是还是会超时。。最后只能去打表- - 无语。。。
#include <stdio.h> int main()
{
int n;
int sb[14];
for (n = 1; n <= 13; n ++)
{
int k;
for (k = n + 1; k < 2000000000; k ++)
{
int num = 2 * n;
int wei = 0;
int judge = 0;
int s = 0;
while (1)
{
wei = (k + wei) % num;
if (wei <= n && wei >= 1)
break;
if (wei == 0)
wei += num;
wei --;
num --;
if (num == n)
{
judge = 1;
break;
}
}
if (judge)
break;
}
sb[n] = k;
}
int m;
while (scanf("%d", &m) != EOF && m)
{
printf("%d\n", sb[m]);
}
return 0;
}
UVA 305 Joseph (约瑟夫环 打表)的更多相关文章
- uva 305 Joseph
点击打开链接uva 305 思路: 数学+打表 分析: 1 传统的约瑟夫问题是给定n个人和m,每次数m次把当前这个人踢出局,问最后留下的一个人的编号 2 这一题是前k个人是好人,后面k个是坏人.现在要 ...
- poj 1012 & hdu 1443 Joseph(约瑟夫环变形)
题目链接: POJ 1012: id=1012">http://poj.org/problem?id=1012 HDU 1443: pid=1443">http:// ...
- 【约瑟夫环变形】UVa 1394 - And Then There Was One
首先看到这题脑子里立刻跳出链表..后来继续看如家的分析说,链表法时间复杂度为O(n*k),肯定会TLE,自己才意识到果然自个儿又头脑简单了 T^T. 看如家的分析没怎么看懂,后来发现这篇自己理解起来更 ...
- Joseph POJ - 1012 约瑟夫环递推
题意:约瑟夫环 初始前k个人后k个人 问m等于多少的时候 后k个先出去 题解:因为前k个位置是不动的,所以只要考虑每次递推后的位置在不在前面k个就行 有递推式 ans[i]=(ans[i-1]+m ...
- 约瑟夫环(Joseph)的高级版(面向事件及“伪链表””)
约瑟夫环问题: 在一间房间总共有n个人(下标0-n-1),只能有最后一个人活命. 按照如下规则去杀人: 所有人围成一圈 顺时针报数,每次报到q的人将被杀掉 被杀掉的人将从房间内被移走 然后从被杀掉的下 ...
- 小小c#算法题 - 12 - Joseph Circle(约瑟夫环)
约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数(从1开始报数),数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又 ...
- hdu 1443 Joseph【约瑟夫环】
题目 题意:一共有2k个人,分别为k个好人和k个坏人,现在我们需要每隔m个人把坏人挑出来,但是条件是最后一个坏人挑出来前不能有好人被挑出来..问最小的m是多少 约瑟夫环问题,通常解决这类问题时我们把编 ...
- hdu1443(约瑟夫环游戏的原理 用链表过的)
Problem Description The Joseph's problem is notoriously known. For those who are not familiar with t ...
- HDU 5643 King's Game 【约瑟夫环】
题意: 变形的约瑟夫环,最初为每个人编号1到n,第i次删去报号为i的人,然后从它的下一个人开始重新从1开始报号,问最终剩下第几号人? 分析: 首先看一下裸的约瑟夫环问题: 共n个人,从1开始报数,报到 ...
随机推荐
- 转:7个鲜为人知却超实用的PHP函数
PHP have lots of built-in functions, and most developers know many of them. But a few functions are ...
- EMV规范 ---ISO7816 T=0协议的时间特性
复位应答期间: 字符间的时间间隔最小是12etu,最大是9600etu,但整个ATR不得超过19200etu(TS的起始沿到最后一个字符的起始沿 从卡片发出的连续字符其最小时间间隔为12etu,但是终 ...
- kibaba 选择字段
- github 提交报403 forbidden的错误解决
github 提交报403 forbidden的错误解决 $ git push error: The requested URL returned error: 403 Forbidden while ...
- [LeetCode] 187. Repeated DNA Sequences 解题思路
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- Google 2013 campus test-R1
Reading Phone Number #include<iostream> #include<fstream> #include<vector> #includ ...
- JavaScript之原型深入详解
理解原型 原型是一个对象,其他对象可以通过它实现属性继承.任何一个对象都可以成为继承,所有对象在默认的情况下都有一个原型,因为原型本身也是对象,所以每个原型自身又有一个原型.任何一个对象都有一个pro ...
- Exception in thread "main" com.sun.xml.internal.ws.streaming.XMLStreamReaderException: unexpected XML tag.
webservice 抛异常,原因: public class HeaderHandler implements SOAPHandler<SOAPMessageContext>{ @Ove ...
- Git详解之一 Git起步
起步 本章介绍开始使用 Git 前的相关知识.我们会先了解一些版本控制工具的历史背景,然后试着让 Git 在你的系统上跑起来,直到最后配置好,可以正常开始开发工作.读完本章,你就会明白为什么 Git ...
- Jenkins 初见
在网上貌似没有找到Jenkins的中文的太多的文档,有的都是关于Hudson的一些 零零散散的,所以自己边学习边实践总结了以下系列文章,希望有助于大家对于Jenkins的使用. 本系列文章是基于我3年 ...