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开始报数,报到 ...
随机推荐
- reaver使用相关
(非TP-LINK路由推荐–d9 –t9参数防止路由僵死 reaver -i mon0 -b MAC -a -S –d9 –t9 -vv) 小结-学习时应因状况调整参数(-c后面都已目标频道为1作为例 ...
- 转:php连接oracle设定字符集,避免乱码
原文来自于:http://muyu.iteye.com/blog/399884 数据库用oracle,当php连接oracle的时候,最好指定字符集. 查PHP手册,oci_connect的第四个参数 ...
- 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 ...
- poj2352 Stars
http://poj.org/problem?id=2352 #include <cstdio> #include <cstring> #define maxn 400000 ...
- JS调试工具
IE http://msdn.microsoft.com/zh-cn/library/ie/dn255003(v=vs.85).aspx FF http://www.wumii.com/item/1g ...
- 使用javascript判断浏览器类型
之前在项目中遇到过要针对不同浏览器做不同的一些js或者css操作,后来某个朋友也突然问到这个问题,所以,整理了一下,在这里留个笔记,方便以后使用. 使用javascript判断浏览器类型: funct ...
- vmware workstation11虚拟机破解版(附安装教程) 32/64位
http://kuai.xunlei.com/d/ru4IAALVJQBesH9U93e?p=20395 vmware workstation 11注册机是一款可以免费生成vmware11.0版本序列 ...
- Apache Tomcat8必备知识
Apache Tomcat8必备知识 作者:chszs,转载需注明.博客主页: http://blog.csdn.net/chszs 一.Apache Tomcat 8介绍 Apache Tomcat ...
- Java TCP服务端向客户端发送图片
/** * 1.创建TCP服务端,TCP客户端 * 2.服务端等待客户端连接,客户端连接后,服务端向客户端写入图片 * 3.客户端收到后进行文件保存 * @author Administrator * ...
- Linux下python升级
Centos即使用Yum更新也是Python2.6.6所以需要升级到Python2.7.8 1.先下载源码包 1 wget https://www.python.org/ftp/python/2.7. ...