队列模拟解决约瑟夫环问题
来源:互联网 发布:sqlserver 数据库管理 编辑:程序博客网 时间:2024/05/01 21:17
//========================the practice of the queue =======================//
/*************************************************
Copyright:all rights reversed
Author: Steven
Date:2010-08-25
Description: Conditions : give you the number 1-N in sequence.
Operation rules : move out the top number in sequence
move the one after the top one to the end of the sequence.
task : give the sequence of the number moved out
**************************************************/
无论是用链表实现还是用数组实现都有一个共同点:要模拟整个游戏过程,不仅程序写起
来
比较烦,而且时间复杂度高达O(nm),当n,m非常大(例如上百万,上千万)的时候,几乎是
没有办法在短时间内出结果的。
#include<iostream>
#include<queue>
using namespace std;
int main()
{
int num_count =0; // the quantity of the number
int num_separate=0;
queue<int> num_queue; // number queue
cout<<"length"<<endl;
cin>>num_count;
cout<<"num_separate"<<endl;
cin>>num_separate;
for(int index=0;index<num_count;index++)
{
num_queue.push(index+1);
}
while(!num_queue.empty())
{
if(num_queue.size()==1)
cout<<num_queue.front()<<endl;
num_queue.pop();
if(num_queue.empty())
break;
else
{
for(int index=1;index<num_separate-1;index++)
{
num_queue.push(num_queue.front());
num_queue.pop();
}
}
}
//cout<<"the last number is: "<<num_queue.front()<<endl;
// num_queue.pop();
system("pause");
return 0;
}
- 队列模拟解决约瑟夫环问题
- 循环队列解决约瑟夫环问题
- 循环队列解决约瑟夫环问题。
- HDU 4841 圆桌问题(约瑟夫环队列模拟)
- 解决下约瑟夫环问题
- 数组解决约瑟夫环问题
- 数组解决约瑟夫环问题
- 约瑟夫环问题的解决
- 约瑟夫环问题[已解决]
- php解决约瑟夫环问题
- C# 解决约瑟夫环问题
- 约瑟夫环问题的两种解决方式(递归求解和数组模拟求解)
- 队列与约瑟夫问题
- 队列实现约瑟夫问题
- 经典模拟问题--约瑟夫环 POJ--3750
- zoj 1088 约瑟夫环问题(模拟)
- 模拟 - 约瑟夫环问题及其变式
- 数组模拟约瑟夫问题
- u盘启动ubuntu系统
- 6/24/2011 5:15:13 PM
- 如何学习c++?请各位指点指点
- 习惯决定命运
- css和js中资源路径问题
- 队列模拟解决约瑟夫环问题
- 启用ashx的session
- r.java文件没有生成
- 业务建模
- 业务建模
- Remove XP Total Security 2012 - how to remove scamware manually
- 我的第一篇文章
- Uninstall Spybot Search - how to uninstall Spybot Search
- 编程之美4.2扩展问题