火车车厢重新排列问题
来源:互联网 发布:java 数字验证码识别 编辑:程序博客网 时间:2024/05/01 07:58
火车车厢重新排序——栈的应用
一、需求分析
二、算法设计
实验结果
三、代码模块
#include <iostream>#include <stack>#include <vector>#include <windows.h>using namespace std;int main(){ int n,m; cout<<"please input the number of train:"<<endl; cin>>n; int *train = new int[n * sizeof(int)]; cout<<"input train number:"<<endl; for(int i=0;i<n;i++){ cin>>m; train[i]=m; } vector< stack<int> > v; int count=1,k=0,total=0; bool suc=false; while(count<=n){ if(k<n && train[k] == count){ cout<<train[k]<<" will leave!"<<endl;; count++; k++; } else{ if(k<n){ int pos=1; for(auto &i:v){ if( i.empty()||train[k]<i.top()){ i.push(train[k]); cout<<train[k]<<" has been pushed in stack["<<pos<<"]!"<<endl; suc=true; k++; break; } pos++; } if(!suc){ stack<int> st; st.push(train[k]); v.push_back(st); cout<<"here will build a new stack and "<<train[k]<<" will be pushed in!"<<endl; k++; total++; } suc=false; } for (int i = 0; i < v.size(); ++i){ while(!v[i].empty() && v[i].top()==count){ cout<<v[i].top()<<" will be poped from stack["<<i+1<<"] and leave!"<<endl; v[i].pop(); count++; } } } } for (int i = 1; i <= n; ++i) { cout<<i<<' '; } cout<<"use "<<total<<" stack"<<endl; delete []train; system("pause"); return 0;}
希望各位有什么好的想法,可以在评论区多多留言,小白衷心感谢各位的意见~
阅读全文
0 0
- 火车车厢重新排列问题
- 火车车厢重排问题
- C++ Stack Example Rearranging RailRoad Cars 火车车厢重排问题
- C++ Queue Example Rearranging RailRoad Cars 火车车厢重排问题
- 火车车厢重排
- 火车车厢重排
- 火车车厢分布图
- 火车车厢分布图
- 火车车厢座位分布指南
- 火车车厢重排/队列实现
- 数据结构(2)——链表形式的堆栈,以及火车车厢重排问题
- 数据结构_链队列实验——火车车厢重排问题
- 数据结构与算法C++描述(7)---堆栈及其在“火车车厢重排问题”中的应用
- 火车硬座车厢座位分布表
- 火车硬座车厢座位分布表
- 火车车厢重排(链队列)
- 火车硬座车厢座位分布表
- 队列的应用--火车车厢重排列
- cuda dynamic parallelism-CUDA动态并行
- 数组的逆序
- UVALive
- 树状数组的玄学功效
- 【知了堂学习笔记】SQL查询总结(1)
- 火车车厢重新排列问题
- 计蒜客-计数问题
- JavaScript忍者秘籍笔记03之正则表达式
- 学习中记扎
- Redis数据结构
- 二叉树的基本操作
- Spring嵌套事物,事物的传播之REQUIRES_NEW
- java日常学习:直接排序与冒泡排序
- 结构体大小计算