llllmz

导航

3254:约瑟夫问题No.2C++

\

这题思路还是挺多的。

如果用数学的角度考虑。知道了n,p,m自然就知道下一个要出队的人的编号。然后一个个输出就行了。

还可以用循环链表做。

还可以用队列。出队在入队。

#include<iostream>
#include<queue>
using namespace std;

int main(){
    int n , p , m ;
    while(cin >> n >> p >> m){
        if(n==0) break;
        queue<int> q;
        for(int i = 1;i<=n;i++){
            q.push(i);
        }
        for(int i =1;i<p;i++){
            int t=q.front();
            q.pop();
            q.push(t);
        }
        int flag=0;
        while(!q.empty()){
            for(int i =1;i<m;i++){
                int t=q.front();
                q.pop();
                q.push(t);
            }
            int t=q.front();
            q.pop();
            if(!flag){
                cout << t;
                flag=1;
            }else{
                cout << ',' << t ;
            }
        }
        cout << '\n';
    }
    return 0;
}

结果:

posted on 2024-01-17 18:31  神奇的萝卜丝  阅读(62)  评论(0)    收藏  举报