POJ 1012 Joseph
2014-10-16 16:28
691 查看
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 |
#include <iostream> using namespace std; int a[20]; int main(){ int n; for(int n=1;n<14;n++){ int nn=2*n; int k=n; int flag; int i=k; while(1){ if(i%nn==0) i=i+k; else i++; //i=5; int p=1; flag=1; // cout<<"i:"<<i<<endl; int q=nn; for(int j=0;j<n;j++){ p=(p+i-1)%q; //cout<<q<<" "<<p<<" "; if(p<=n&&p!=0){ flag=0; break; } if(p==q||p==0) p=1; q--; } if(flag==1){ break; } } a =i; } int m; while(cin>>m&&m){ cout<<a[m]<<endl; } return 0; }
相关文章推荐
- POJ 1012 Joseph
- poj 1012 & hdu 1443 Joseph(约瑟夫环变形)
- [POJ1012]Joseph
- poj1012-Joseph
- POJ 1012 Joseph
- POJ 1012 Joseph
- POJ 1012(Joseph)
- poj 1012 & hdu 1443 Joseph(约瑟夫环变形)
- POJ 1012: Joseph
- poj 1012 Joseph
- poj1012-Joseph
- POJ 1012 Joseph 约瑟夫问题
- POJ1012 Joseph
- 【原】 POJ 1012 Joseph 约瑟夫 解题报告
- POJ 1012 Joseph 约瑟夫问题
- poj 1012 Joseph
- Poj1012_Joseph
- POJ 1012 Joseph 约瑟夫问题
- poj 1012 Joseph环问题
- poj 1012 -- Joseph (约瑟夫环)