Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
约瑟夫问题
#include <iostream>
using namespace std;
int main()
{
unsigned long k;
cin >> k;
while (k != 0)
{
for (unsigned long m = 2;; m++)
{
bool right = 1;
bool kicked[30] = {0};
unsigned long call = 1;
unsigned long position = 1;
unsigned long lastposition = 1;
unsigned long kick = 0;
while (kick < k)
{
unsigned long m1 = (m % (2 * k - kick));
if (m1 == 0)
{
m1 = 2 * k - kick;
}
for (call = 1; call <= m1;)
{
if (!kicked[position])
{
call++;
lastposition = position;
}
position++;
if (position > 2 * k)
{
position = 1;
}
}
if (lastposition <= k)
{
right = 0;
break;
}
kicked[lastposition] = 1;
kick++;
}
if (right)
{
cout << m << endl;
break;
}
}
cin >> k;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment