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开始报数,报到 ...
随机推荐
- Zend Cache的学习和实例
前一段时间,公司让我组织一下关于Zend Cache的培训. 培训的具体内容有: 前端core缓存 前端Output缓存 前端Function缓存 前端Class缓存 前端File缓存 前端Page缓 ...
- 初识MVC模式
一.概念:MVC是 模型(Model),视图(View)和控制(Controller)的缩写,其目的实现Web系统的职能分工. 二.问题描述:通过一个简单的例子(计算圆面积)来体会MVC模式和普通的s ...
- grunt serve Fatal error: Port 35729 is already in use by another process.
y@y:~$ lsof | grunt y 0t0 TCP *: (LISTEN) Optimizin y 0t0 TCP *: (LISTEN) v8:Sweepe y 0t0 TCP *: (LI ...
- 【动态规划】XMU 1560 新ACM规则
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1560 题目大意: 给定n(n<=200)个任务及每个任务的耗时,问m(m< ...
- bzoj3890 [Usaco2015 Jan]Meeting Time
Description Bessie and her sister Elsie want to travel from the barn to their favorite field, such t ...
- Intel 英特尔
英特尔 英特尔 基本资料 公司名称:英特尔(集成电路公司) 外文名称:Intel Corporation(Integrated Electronics Corporation) 总部地 ...
- springmvc对jsonp的支持
在与前端开发人员合作过程中,经常遇到跨域名访问的问题,通常我们是通过jsonp调用方式来解决.jsop百科:http://baike.baidu.com/link?url=JKlwoETqx2uuKe ...
- 数据库 —— mySQL 的安装
[转载Link]MySQL-5.6.24免安装版配置方法,有需要的朋友可以参考下. 1. 下载MySQL Community Server 5.6.24 2. 解压MySQL压缩包 将以下载的MySQ ...
- 更改navigationController push和pop界面切换动画
For Push: MainView *nextView=[[MainView alloc] init]; [UIView beginAnimations:nil context:NULL]; [UI ...
- [Angular 2] Managing State in RxJS with StartWith and Scan
The scan operator in RxJS is the main key to managing values and states in your stream. Scan behaves ...