Joseph
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 54669 |
|
Accepted: 20916 |
Description the Joseph ' s problem is notoriously known. For those who is familiar with the original Problem:from among n people, numbered 1, 2, ..., n, standing in circle Every mth is going to being executed and only the last remaining person would be saved. Joseph is smart enough to choose the position of the last remaining person, thus saving he life to give us the message a Bout the incident. For example if n = 6 and M = 5 then the people would be executed in the order 5, 4, 6, 2, 3 and 1 would be saved.
Suppose that there is k good guys and k bad guys. In the circle the first k is good guys and the last k bad guys. You had to determine such minimal m so all the bad guys would 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 would consist of separate lines containing m corresponding to K in the input file.
Sample Input
3
4
0
Sample Output
5
30
Translation
2k person in a circle, the front k person is a good person, the back of K is a bad person, the loop count, the input m for this in m personal stop, count on the person to die, our goal is to first kill all the bad guys, please find M
Shortcut
#include <iostream>
using namespace std;
int i;//assumed value m
int k;//k good guys
int joseph[20];
int kgood (int i_c,int kgood,int Kbad,int index) { //i__m;index__ loops how many times __ cycles Kbad times ends
if (index==k) return 1;
int x=i_c% (Kgood+kbad); The current turn of the person
if (x==0) x= (Kgood+kbad);
int y= (Kgood+kbad)-X; How many bad guys
if (X<=kgood) return 0 is behind the turn of the person; If the turn good guys is not this number
Kgood (i-y,kgood,kbad-1,index+1);
}
int main () {
// freopen ("E:1001.txt", "R", stdin);
while ((cin>>k) && k!=0)
{
if (joseph[k]!=0)
{
Cout<<joseph[k]<<endl ;
}
for (i=1;i<int_max;i++)
{
if (Kgood (i,k,k,0) ==1) break ;
}
joseph[k]=i;
cout<<i<<endl;
}
}
This is not known Joseph ring practice
Using this pure brute force method to get 1-13 first, that note down, then case 1=5,case 2=30, write it down and then submit the code, I was the first water ...
Code
We need to know about Joseph Ring. Click the Open link, click the Open link
#include <iostream>
#include <cstring>
using namespace std;
int main () {
int person[30];
int joseph[15];
memset (person,0,sizeof (person));
memset (Joseph,0,sizeof (Joseph));
int K;
while ((cin>>k) && k!=0)
{
if (joseph[k]!=0) {
cout<<joseph[k]<<endl;
Continue;
}
int numofperson=2*k;
int m=k+1;//Kill the person//minimum is definitely k+1, the maximum is 2K
memset (person,0,sizeof (person));//clear the//for
(int i=1;i<=k;i++)
{
person[i]= (person[i-1]+m-1)% (numofperson-i+1);
if (person[i]<k)//good guys can not kill
{
m++;
i=0;//i is not = 1, because there are i++
}
}
joseph[k]=m
in the back; cout<<m<<endl;
}
}