POJ 1012 Joseph
2015-08-19 09:17
369 查看
Language: 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 Source Central Europe 1995 ACcode: #include <iostream> #include <cstdio> using namespace std; int main(){ int a[14],i,j,k,temp; for(i=1;i<14;++i){ for(j=i+1;;j++){ for(k=0,temp=0;k<i;k++){ temp=(temp+j-1)%(2*i-k); if(temp<i)break; } if(i==k){ a[i]=j; break; } } } while(scanf("%d",&i)!=EOF&&i)printf("%d\n",a[i]); return 0; } |
相关文章推荐
- AsyncTask doinbackground onProgressUpdate onCancelled onPostExecute的基本使用
- Java和C++ 比较
- ssh 不能update del add
- TESIS VEHDYN33
- android通知 notification
- 如何创建一个静态库文件,并打包在模拟器和真机上都可以使用。
- python笔记:深刻理解Python中的元类(metaclass)
- hdu5399
- poj-3126 Prime Path
- 算法竞赛入门经典:第八章 高效算法设计 8.9棋盘覆盖
- Metronic 定制主题
- 互斥锁设计,有效的避免死锁
- CentOs笔记
- 对List进行高效的排序和倒排序代码实战之Scala学习笔记-26
- tools:context=".MainActivity的作用
- git 提交前撤销add操作
- OC类的深入研究
- poj2253 最小生成树中的最大边 prim
- LeetCode Binary Tree Preorder Traversal
- HDU 5400 Arithmetic Sequence(2015 Multi-University Training Contest 9 2015多校联合)