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 (约瑟夫环 打表)的更多相关文章

  1. uva 305 Joseph

    点击打开链接uva 305 思路: 数学+打表 分析: 1 传统的约瑟夫问题是给定n个人和m,每次数m次把当前这个人踢出局,问最后留下的一个人的编号 2 这一题是前k个人是好人,后面k个是坏人.现在要 ...

  2. poj 1012 &amp; hdu 1443 Joseph(约瑟夫环变形)

    题目链接: POJ  1012: id=1012">http://poj.org/problem?id=1012 HDU 1443: pid=1443">http:// ...

  3. 【约瑟夫环变形】UVa 1394 - And Then There Was One

    首先看到这题脑子里立刻跳出链表..后来继续看如家的分析说,链表法时间复杂度为O(n*k),肯定会TLE,自己才意识到果然自个儿又头脑简单了 T^T. 看如家的分析没怎么看懂,后来发现这篇自己理解起来更 ...

  4. Joseph POJ - 1012 约瑟夫环递推

    题意:约瑟夫环  初始前k个人后k个人  问m等于多少的时候 后k个先出去 题解:因为前k个位置是不动的,所以只要考虑每次递推后的位置在不在前面k个就行 有递推式 ans[i]=(ans[i-1]+m ...

  5. 约瑟夫环(Joseph)的高级版(面向事件及“伪链表””)

    约瑟夫环问题: 在一间房间总共有n个人(下标0-n-1),只能有最后一个人活命. 按照如下规则去杀人: 所有人围成一圈 顺时针报数,每次报到q的人将被杀掉 被杀掉的人将从房间内被移走 然后从被杀掉的下 ...

  6. 小小c#算法题 - 12 - Joseph Circle(约瑟夫环)

    约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数(从1开始报数),数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又 ...

  7. hdu 1443 Joseph【约瑟夫环】

    题目 题意:一共有2k个人,分别为k个好人和k个坏人,现在我们需要每隔m个人把坏人挑出来,但是条件是最后一个坏人挑出来前不能有好人被挑出来..问最小的m是多少 约瑟夫环问题,通常解决这类问题时我们把编 ...

  8. hdu1443(约瑟夫环游戏的原理 用链表过的)

    Problem Description The Joseph's problem is notoriously known. For those who are not familiar with t ...

  9. HDU 5643 King's Game 【约瑟夫环】

    题意: 变形的约瑟夫环,最初为每个人编号1到n,第i次删去报号为i的人,然后从它的下一个人开始重新从1开始报号,问最终剩下第几号人? 分析: 首先看一下裸的约瑟夫环问题: 共n个人,从1开始报数,报到 ...

随机推荐

  1. C语言学习笔记--字符串函数

    字符串函数 需要包含头文件#include<stdio.h> strlen strcmp strcpy strchr strstr strcasestr

  2. logstash 处理tomcat catalina.out

    input { file { type => "zj_api" path => ["/data01/applog_backup/zjzc_log/zj-api ...

  3. 【HDOJ】4956 Poor Hanamichi

    基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. #include <cstdio> int f(__int64 x) { int i, sum; i = ...

  4. 设计模式(一): abstract factory抽象工厂模式 -- 创建型模式

    1.定义 为创建一组相关或相互依赖的对象提供一个接口,而且无需指定他们的具体类. 2.适用场景 1.一个系统要独立于它的产品创建.组合和表示. 2.一个系统要由多个产品系列中的一个来配置. 3.当你要 ...

  5. 2015第35周五JavaScript变量

    java语言里有一句很经典的话:在java的世界里,一切皆是对象. Javascript虽然跟java没有半点毛关系,但是很多会使用javascript的朋友同样认为:在javascript的世界里, ...

  6. HBase MultiVersionConsistencyControl

    注明:本文部分文字和图片翻译或引用自http://blogs.apache.org/hbase/entry/apache_hbase_internals_locking_and. HBase在保证高性 ...

  7. Android之路-------Activity的详解

    前言 由于接近放假,公司在赶项目所以前段LP比较忙,没什么时间总结和写博客,只是准备睡觉的时候看看书,每天看的不算多,大概10多页左右吧,不过每天坚持如此的话那也是一个庞大的数字. 今天LP的任务完成 ...

  8. Google 2013 campus test-R1

    Reading Phone Number #include<iostream> #include<fstream> #include<vector> #includ ...

  9. 弱爆了的Candies

    题目出处 题目描述: n个小朋友坐成一排,每个小朋友有一个数表示他的表现(数字越大表现越好).老师要给每个小朋友发至少1颗糖,相邻的两个小朋友,得分较高的小朋友必须得到更多的糖,问:老师至少需要给出多 ...

  10. 大龄剩女四大结局:孤寡 后妈 拉拉 出家 宽带山KDS-宽带山社区-第一城市消费门户

    大龄剩女四大结局:孤寡 后妈 拉拉 出家 宽带山KDS-宽带山社区-第一城市消费门户 主题:大龄剩女四大结局:孤寡 后妈 拉拉 出家