题解 | #约瑟夫环#

约瑟夫环

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;
}
全部评论

相关推荐

10-24 13:36
门头沟学院 Java
Zzzzoooo:更新:今天下午有hr联系我去不去客户端,拒了
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务