poj 1012 & hdu 1443 Joseph(约瑟夫环变形)
题目链接:
POJ 1012: id=1012">http://poj.org/problem?id=1012
HDU 1443: pid=1443">http://acm.hdu.edu.cn/showproblem.php? pid=1443
约瑟夫环(百度百科): fr=aladdin" target="_blank">http://baike.baidu.com/view/717633.htm?fr=aladdin
Description
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
Output
Sample Input
3
4
0
Sample Output
5
30
Source
题意:
共同拥有 k 个坏人 k 个好人坐成一圈(总人数就为2*k)。前面的 k 个为好人(编号1~k),后面的 k 个为坏人(编号k+1~2k),
现有一个报数为 m ,从编号为 1 的人開始报数。报到 m 的人会自己主动的死去。
求当 m 为何值时。能够使在出现有好人死亡前,k 个坏人已经所有死掉?
注意: 当前一轮第 m 个人死后,下一轮的编号为1的人是前一轮编号为 m+1 的人。
若前一轮恰好是最后一个人死掉,则下一轮循环回到开头那个人报“1”
k比較小,直接暴力打表!
代码例如以下:
#include <cstdio>
int main()
{
int p[17], ans[17] = {0};
int m, k;
for(k = 1; k <= 14; k++)
{
int n = 2*k;//总人数
m = 1;
for(int i = 1; i <= k; i++)
{
//由于编号是从1開始的
ans[i] = (ans[i-1]+m-1)%(n-i+1);
if(ans[i] < k)//说明杀掉了好人,不符合题意
{
i = 0;
m++;//枚举
}
}
p[k] = m;
// printf("%d\n",p[k]);
}
// return 0;
int kk;
while(scanf("%d",&kk) && kk)
{
printf("%d\n",p[kk]);
}
return 0;
}
poj 1012 & hdu 1443 Joseph(约瑟夫环变形)的更多相关文章
- hdu 1443 Joseph【约瑟夫环】
题目 题意:一共有2k个人,分别为k个好人和k个坏人,现在我们需要每隔m个人把坏人挑出来,但是条件是最后一个坏人挑出来前不能有好人被挑出来..问最小的m是多少 约瑟夫环问题,通常解决这类问题时我们把编 ...
- HDU 3089 (快速约瑟夫环)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3089 题目大意:一共n人.从1号开始,每k个人T掉.问最后的人.n超大. 解题思路: 除去超大的n之 ...
- Hdu 1443 Joseph
Joseph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- Poj 3517 And Then There Was One(约瑟夫环变形)
简单说一下约瑟夫环:约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个 ...
- HDU 5643 King's Game | 约瑟夫环变形
经典约瑟夫环 }; ; i<=n; i++) { f[i] = (f[i-] + k) % i; } 变形:k是变化的 #include <iostream> #include &l ...
- 【约瑟夫环变形】UVa 1394 - And Then There Was One
首先看到这题脑子里立刻跳出链表..后来继续看如家的分析说,链表法时间复杂度为O(n*k),肯定会TLE,自己才意识到果然自个儿又头脑简单了 T^T. 看如家的分析没怎么看懂,后来发现这篇自己理解起来更 ...
- F - System Overload(约瑟夫环变形)
Description Recently you must have experienced that when too many people use the BBS simultaneously, ...
- hdu 1443 Joseph (约瑟夫环)
Joseph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- (顺序表的应用5.4.2)POJ 1591 M*A*S*H(约瑟夫环问题的变形——变换步长值)
/* * POJ_1591_2.cpp * * Created on: 2013年10月31日 * Author: Administrator */ #include <iostream> ...
随机推荐
- JsCal( JS Calendar)
http://www.dynarch.com/projects/calendar Doc: http://www.dynarch.com/jscal/ This is the documentatio ...
- 浅谈Java中的System.gc()的工作原理
很多人把Java的“效率低下”归咎于不能自由管理内存,但我们也知道将内存管理封装起来的好处,这里就不赘述. Java中的内存分配是随着new一个新的对象来实现的,这个很简单,而且也还是有一些可以“改进 ...
- Angularjs学习笔记1_基本技巧
10.AngularJS ng-click <button ng-click="clickCounter = clickCounter + 1">Click Me! ...
- 从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展添加检查订单功能,以便记录并检測输入的參数
这一章节我们讨论一下扩展添加检查订单功能,以便记录并检測输入的參数. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03 ...
- C++语言基础(19)-模板的显式具体化
应用背景: 例如有下面的函数模板,它用来获取两个变量中较大的一个: template<class T> const T& Max(const T& a, const T&a ...
- 11.static(转)
本文转自:http://blog.csdn.net/keyeagle/article/details/6708077 google了近三页的关于C语言中static的内容,发现可用的信息很少,要么长篇 ...
- AngularCSS 的引入: CSS On-Demand for AngularJS
1) Include the required JavaScript libraries in your index.html (ngRoute and UI Router are optional) ...
- C#虚方法、抽象类、方法重写
Timer.每隔一段时间触发一个事件.不可视控件.Interval.Enabled.Tick事件.计量单位:ms(毫秒). 1秒=1000毫秒.取当前时间 DateTime.Now.ToSt ...
- Scrapy爬虫入门系列4抓取豆瓣Top250电影数据
豆瓣有些电影页面需要登录才能查看. 目录 [隐藏] 1 创建工程 2 定义Item 3 编写爬虫(Spider) 4 存储数据 5 配置文件 6 艺搜参考 创建工程 scrapy startproj ...
- TelephonyManager&GsmCellLocation类的方法详解
转载:http://blog.163.com/zhangzheming_282/blog/static/117920962011101944356511/ TelephonyManager类 主要提供 ...