2020-06-28 约瑟夫问题

2020-06-28  本文已影响0人  JalorOo
#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;
}

上一篇下一篇

猜你喜欢

热点阅读