约瑟夫问题(队列)

约瑟夫是一个无聊的人!!!

题目描述

n个人(n<=100)围成一圈,从第一个人开始报数,数到m的人出列,再由下一个人重新从1开始报数,数到m的人再出圈,……依次类推,直到所有的人都出圈,请输出依次出圈人的编号.

输入输出格式

输入格式:

n m

输出格式:

出圈的编号

输入输出样例

输入样例#1: 复制

10 3

输出样例#1: 复制

3 6 9 2 7 1 8 5 10 4

说明

m,n≤100m, n le 100m,n≤100

这个题是个非常水的题,只需要队列模拟这个过程就行,只不过要注意0 0这种特殊情况,下面是ac代码

写的不好,多多担待

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
queue<int>q;
int main()
{

	int n,m;
	cin>>n>>m;
	if(n==0&&m==0)
	{
		cout<<endl;
	}//0 0情况直接拿出
	else
	{
	
	for(int t=1;t<=n;t++)
	{
		q.push(t);
	}//让这n个数入队
	for(int t=0;t<m-1;t++)
       {
	   q.push(q.front());
           q.pop();
       }
        //然后把开头的数放到队尾,
        //然后出队
        //这个过程可以在纸上画画就会理解
       cout<<q.front();
       q.pop();
     while(!q.empty())
     {
     	for(int t=0;t<m-1;t++)
       {
	   q.push(q.front());
       q.pop();
       }
       cout<<" "<<q.front();
       q.pop();
	 }
	 cout<<endl;
}
	return 0;
 } 
原文地址:https://www.cnblogs.com/Staceyacm/p/10782109.html