/*约瑟夫环问题
m人报n数
*/
#include <stdio.h>
#include <stdlib.h>
#define N 100
int main (void)
{
int i, j, count = 0, var = 0, tem;
int person[N];
int m, n;
while (scanf ("%d%d", &m, &n) != EOF)
{
for (i = 0; i < m; i++)
{
person[i] = 1;
}
for (i = 0; var != 1; i++, i = ((i + 1) > m ? 0 : i))
{
if (person[i])
{
count++;
if (!(count % n))
{
person[i] = 0;
}
}
var = 0;
for (j = 0; j < m; j++)
{
if (person[j])
{
var++;
tem = j;
}
}
}
printf ("%d", tem);
}
system ("pause>nul");
return 0;
}
约瑟夫环问题
最新推荐文章于 2022-05-04 19:46:17 发布