题解 | #约瑟夫环#
约瑟夫环
https://ac.nowcoder.com/acm/problem/22227
#include <stdio.h>
int main()
{
int n, k, m;
scanf("%d %d %d", &n, &k, &m);
int i=0,j=1,cnt=0;
int a[10000];
for (i = 0; i < n; i++)
{
a[i] = 1;
}
i = k;
while(cnt!=n-1)
{
if (i>n-1||j>m)
{
if(i>n-1)
i = 0;
if(j>m)
j=1;
continue;
}
while(a[i]==0)
{
i++;
}
if (i>n-1||j>m)
{
if(i>n-1)
i = 0;
if(j>m)
j=1;
continue;
}
if(j==m)
{
a[i] = 0;
j = 1;
cnt++;
i++;
continue;
}
j++;
i++;
if (i > n-1 ||j>m)
{
if(i>n-1)
i = 0;
if(j>m)
j=1;
}
}
for (i = 0; i < n; i++)
{
if (a[i]!=0)
printf("%d\n", i);
}
return 0;
}
int main()
{
int n, k, m;
scanf("%d %d %d", &n, &k, &m);
int i=0,j=1,cnt=0;
int a[10000];
for (i = 0; i < n; i++)
{
a[i] = 1;
}
i = k;
while(cnt!=n-1)
{
if (i>n-1||j>m)
{
if(i>n-1)
i = 0;
if(j>m)
j=1;
continue;
}
while(a[i]==0)
{
i++;
}
if (i>n-1||j>m)
{
if(i>n-1)
i = 0;
if(j>m)
j=1;
continue;
}
if(j==m)
{
a[i] = 0;
j = 1;
cnt++;
i++;
continue;
}
j++;
i++;
if (i > n-1 ||j>m)
{
if(i>n-1)
i = 0;
if(j>m)
j=1;
}
}
for (i = 0; i < n; i++)
{
if (a[i]!=0)
printf("%d\n", i);
}
return 0;
}