POJ 1012 Joseph
2016-05-22 10:48
183 查看
Joseph
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
Sample Output
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 52102 | Accepted: 19840 |
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 自己写的枚举超时的不得了 借鉴了wmy大神的代码(虽然也超时,但是快多了):
/*------------------------------分割线----------------------------------*/ #include<iostream> using namespace std; #include<cstdio> int n,k,m; int main() { while(scanf("%d",&k)==1) { if(k==0) break; for(m=k+1;;m++) { n=2*k; int p=0; bool flag=true; for(int i=1;i<=k;++i) {/*我在这里用了暴力,挨个挨个的数*/ p=(p+m-1)%n;/*最巧妙的在于这句,p表示删除人在之前被删的序列(不是原序列)的编号,这个应该很好理解,每次人数减少,所以总人数n也--*/ if(p<k)/*如果我们删除的对的话,那么前k个人的编号是不变的,所以p始终是不能等于p的*/ { flag=false; break; } n--; } if(flag) { printf("%d\n",m); break; } } } return 0; } /*------------------------------分割线----------------------------------*/ 打表代码: #include<iostream> using namespace std; int pp[15]={0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881}; int main() { int k; while(cin>>k) { if(k==0) break; else cout<<pp[k]<<endl; } return 0;; }
相关文章推荐
- 软件项目进度管理
- 面积(area)
- Django基础(三)
- LeetCode:Intersection of Two Arrays II
- StrBuffer 类的应用---暗文的解密
- android避免OOM的几种常见方式
- HDU 5694 BD String 迭代
- FCM压缩算法
- github命令大全
- centos6.5卸载自带mysql
- 《Zero to One》读书笔记
- NEFU 1160 线段树
- 封装ajax——前端工程师和派孔明
- 分层开发MySchool总结
- hadoop2.6.0版本集群环境搭建
- 相对布局(RelativeLayout)
- AlertDialog
- hdu1059 Dividing
- SSH学习笔记
- JVM内存管理和垃圾回收