博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
杭电_ACM_Joseph
阅读量:6413 次
发布时间:2019-06-23

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

Problem 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
View Code

pay attention

firstly, get result form in advance will take less time than according input to get the result.

secondly, form a habit of recall the method to get the result, I think you will have more clear mind.

thirdly, think in detailly.case in point: you must judge whether index is equal to zero or not, even though you will use get only one "if" to get the answer, but you can't. because the dividend is changed!!

forthly, ok, I must acknowledge that I forget the & and EOF, I really must work hard.

last but not the least, you need not care about which number is  counted, just care about whether the counted number is smaller than k or not.

 

my programme is designed that the number is from 1 to 2 * k

if the number is from 0 to 2 * k - 1, then the code is as follow

View Code
1 int joseph(int k, int m) 2 { 3     int n, index, i; 4     n = 2 * k; 5     index = 0; 6     //circulate k time, if every time the index is bigger than k, then return true 7     for (i = 0; i < k; i++) 8     { 9         index = (index + m - 1) % n;10         n--;11         if (index < k)12             return 0;13     }14     return 1;15 }

转载于:https://www.cnblogs.com/chuanlong/archive/2012/10/28/2743171.html

你可能感兴趣的文章
计算机是怎么存储数字的
查看>>
github精选:微信小程序开发技巧(12月31日更新)2016
查看>>
struts2 中文 url参数
查看>>
CentOS 6系统优化脚本
查看>>
shell 脚本练习3
查看>>
android之首选项相关 Preferences(二)组织首选项
查看>>
两天时间,安装kivy环境,python3.5不行,只能用python2.7
查看>>
移动电商成电商重点市场
查看>>
Spring MVC数据校验(使用@Validated对@RequestParam参数校验)
查看>>
以中国电影市场托底的阿里影业,国际化算盘打的响
查看>>
ipvsadm命令参考
查看>>
实现loading的代码
查看>>
javascript中关于变量定义及范围
查看>>
MySQL 8.0新特性--skip scan range access method(七)
查看>>
Here Document
查看>>
MySQL高可用性之keepalived+mysql双主
查看>>
LVS类型之NAT
查看>>
SQL SERVER 2005如何建立自动备份的维护计划
查看>>
权限及权限管理
查看>>
linux文件系统
查看>>