博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1012 Joseph
阅读量:6717 次
发布时间:2019-06-25

本文共 1859 字,大约阅读时间需要 6 分钟。

 

Description

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

340

Sample Output

530

Source

 
    题目大致意思:就是一个约瑟夫问题。
    1.我们已知前面k个人是好人,后面k个人是坏人
    2.坏人一定要死在好人前面,就是说前面k被杀死的人只能是坏人
    3.由于坏人都在后面,所以杀死任何一个坏人,好人的位置都不会有变化
    4.下一个被杀的人的相对位置:

    5.我们只要保证s>=k,那么就不会杀掉好人

    6. 测试数据比较多,如果每输入一个k就算一次m特别耗时间,所以先打表

 

代码如下:

#include 
using namespace std;int Joseph(int m,int k){ int left = 2 * k; int s = 0; for (int i = 0; i < k; i++) { s = (s + m - 1) % (left - i); if (s < k) return 0; } return 1;}int main(){ int m, k; int a[20]; for (k = 1; k <= 14; k++) //题目给定0
<14 { if (k == 1) a[k] = 2; for (int m = 1;; m++) { if (Joseph(m, k)) { a[k] = m; break; } } } while (cin >> k) { cout << a[k] << endl; }}

 

     

 

 

    

转载于:https://www.cnblogs.com/LHJL8023/p/8003822.html

你可能感兴趣的文章
从Vue.js源码看nextTick机制
查看>>
前端如何处理emoji表情
查看>>
Git Client 安装及 SSH 公钥配置
查看>>
flutter接入现有的app详细介绍
查看>>
vue的虚拟dom
查看>>
如何设置一个本地测试服务器?
查看>>
iOS11以上 获取系统剩余可用空间不准确
查看>>
警告忽略
查看>>
Python3 CookBook | 迭代器与生成器
查看>>
深入理解 Android 中的各种 Context
查看>>
Android 6 0 运行时权限处理解析
查看>>
JavaScript引用类型之Array类型API详解
查看>>
数据库事务和MVCC多版本并发控制
查看>>
自定义控件实践-倒计时控件
查看>>
《JavaScript高级程序设计(第三版)》
查看>>
随手记 - Springboot Application Properties 值
查看>>
java B2B2C Springcloud多租户电子商城系统- 分布式事务
查看>>
屏幕方向读取与锁定:Screen Orientation API
查看>>
记:解决angular报错'Missing locale data for the locale "zh-cn"
查看>>
【半月刊 2】前端高频面试题及答案汇总
查看>>