【经典题目】约瑟夫问题

#include<cstdio>
#include<iostream>
#include<queue>  
using namespace std;
queue<int>q;
int main(){
	int m,n;cin>>n>>m;
	for(int i=1;i<=n;i++)q.push(i);//入队
	
	//开始模拟
	int cnt=1;
	while(!q.empty()){
		if(cnt%m!=0){
			cnt++;
			q.push(q.front());//将暂时不需要出队的元素压入队尾 
			q.pop();
		}
		else{
			cout<<q.front()<<' ';
			q.pop();
			cnt++;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/melon_sama/article/details/108560023