美文网首页
2020-06-28 约瑟夫问题

2020-06-28 约瑟夫问题

作者: JalorOo | 来源:发表于2020-06-28 22:55 被阅读0次
    #include <cstdio>
    #include <iostream>
    
    #define SIZE 105
    using namespace std;
    
    int read(){
        int x = 0, f =1;
        char c = getchar();
        while (c<'0'||c>'9') {
            if (c=='-') {
                f = -1;
            }
            c = getchar();
        }
        while (c>='0'&&c<='9') {
            x = x*10 + c - '0';
            c = getchar();
        }
        return x*f;
    }
    
    int queue[SIZE];
    
    int main(){
        int n = read(),m = read();
        int count = 0;
        int step = 1;
        for (int i = 1; count!=n;) {
            if (i>n) {
                i = 1;
            }
            if (step>m) {
                step=1;
            }
            if (queue[i]==1) {
                //cout<<"p:"<<i<<"跳"<<endl;
                i++;
                continue;
            }
            if (step==m) {
                printf("%d ",i);
                queue[i]=1;
                count++;
            }
            //cout<<"p:"<<i<<" "<<step<<endl;
            i++;
            step++;
        }
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:2020-06-28 约瑟夫问题

          本文链接:https://www.haomeiwen.com/subject/pvogfktx.html