首先声明,我是一个菜鸟。一下文章中出现技术误导情况盖不负责

Look at figure 1. The railroad cars  in the "Input track" is not sorted. We need them in a sorted way  in "Output track" like cars in figure2.

There are three holding tracks we can use to rearranging roailroad cars. A picture is worth than a thousand words, so look at the gif picture below. You can see how the process acts.

So, how we use code to solve the problem? Here three holding tracks in the picture, so we can use three stacks to hold the number.

The Whole Code

每日一道理
即使青春是一枝娇艳的花,但我明白,一枝独放永远不是春天,春天该是万紫千红的世界。 即使青春是一株大地伟岸的树,但我明白,一株独秀永远不是挺拔,成行成排的林木,才是遮风挡沙的绿色长城。即使青春是一叶大海孤高的帆,但我明白,一叶孤帆很难远航,千帆竞发才是大海的壮观。
// RailRoad.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include <iostream>
#include <stack>using namespace std;template <class T>
void PrintfNum(T a[], const int & n);// move cars from holding track to output track
void OutPut(stack<int> t[],int n, int totalStack,int& min){//move car from holding trackfor(int x = 0;x < totalStack; ++x){if(!t[x].empty() && t[x].top() == min){cout << "Move car " << t[x].top() << " from holding track " << x << " to output" << endl;t[x].pop();++min;x = -1; // find next car from the first holding track 0}}
}// move cars from input track to holding track
bool Hold(stack<int> t[],int n , int totalStack){for(int i = 0;i < totalStack; ++i){if(t[i].empty() || (!t[i].empty() && t[i].top() > n)){cout << "holding track " << i << " hold car " << n  << endl;t[i].push(n);return true; // we already find a holding track, so break the loop. }}return false;
}int main(int argc, char* argv[])
{const int NUM = 9;const int STACKNUM = 3;stack<int> t[STACKNUM];int min = 1;int a[NUM] = {5,8,1,7,4,2,9,6,3};PrintfNum(a,NUM);for(int i = NUM - 1; i >= 0; --i){if(a[i] == min){// try to move cars from input track or holding trackcout << "Move car " << a[i] << " from input to output" << endl;++min;OutPut(t,a[i],STACKNUM,min);           }else{// move cars from input track to holding trackif(!Hold(t,a[i],STACKNUM)){cout << "Not enough holding track" << endl;break;}        }}return 0;
}template <class T>
void PrintfNum(T a[], const int & n){for(int i = 0; i < n; ++i){cout << a[i] << ",";}cout << endl;
}

While three holding tracks are sufficient to rearrange the cars from the initial ordering of Figure 1, other initial arrangements may need more tracks. For example, the initial arrangement 1, 9, 8, 7, 6, 5, 4, 3, 2 requires 8 holding tracks.

http://www.waitingfy.com/?p=508

文章结束给大家分享下程序员的一些笑话语录: 一程序员告老还乡,想安度晚年,于是决定在书法上有所造诣。省略数字……,准备好文房4宝,挥起毛笔在白纸上郑重的写下:Hello World

转载于:https://www.cnblogs.com/jiangu66/archive/2013/05/07/3065785.html

nullnullC++ Stack Example Rearranging RailRoad Cars 火车车厢重排问题相关推荐

  1. java队列火车厢重排_火车车厢重排问题--队列模拟

    ①问题描述 一列货运列车共有n节车厢,每节车厢将停放在不同的车站.假定n个车站的编号分别为1-n,即货运列车按照第n站至第1站的次序经过这些车站.为了便于从列车上卸掉相应的车厢,车厢的编号应与车站的编 ...

  2. java队列火车厢重排_火车车厢重排(链队列)

    1.题目: Problem Description 一列货运列车共有n节车厢,每节车厢将停放在不同的车站.假定n个车站的编号分别为1~n,即货运列车按照第n站至第1站的次序经过这些车站.为了便于从列车 ...

  3. 堆栈应用(三):火车车厢重排

    1.问题描述 一列货运列车共有 n节车厢,每节车厢将停放在不同的车站.假定 n个车站的编号分别为1 ~n,货运列车按照第 n站至第 1 站的次序经过这些车站.车厢的编号与它们的目的地相同.为了便于从列 ...

  4. 072.火车车厢重排

    #include "stdafx.h" #include "stdio.h" #include "iostream.h" #include ...

  5. 数据结构-火车车厢重排问题(队列实现)

    问题描述 转轨站示意图如下: 重排过程如下: 伪代码 1. 分别对k个队列初始化: 2. 初始化下一个要输出的车厢编号nowOut = 1; 3. 依次取入轨中的每一个车厢的编号: 3.1 如果入轨中 ...

  6. java队列火车厢重排_火车车厢重排——队列实现

    其实队列和栈挺像的,所以也就没有单独写一个实现队列的笔记,可以参考一下栈的实现:https://www.cnblogs.com/2015-16/p/12957964.html    (同时这一篇也包含 ...

  7. java队列火车厢重排_火车车厢重排问题

    1 #include 2 #include 3 usingstd::stack;4 usingstd::cin;5 usingstd::cout;6 7 const int MAX = 100; // ...

  8. 队列的应用--火车车厢重排列

    火车车厢重排列问题 问题描述:一列货运列车共有n节车厢,每节车厢将停放在不同的车站.假定n个车站的编号为1--n,即货运列车按照第n站至第1站的次序经过这些车站.为了便于列车卸掉相应的车厢,车厢的编号 ...

  9. 数据结构与算法 汉诺塔问题和列车车厢重排问题

    1. 汉诺塔问题: (a)通过递归的方式解决:https://blog.csdn.net/zj1131190425/article/details/85156570 // 汉诺塔问题: 递归解决方案 ...

最新文章

  1. TCP/IP基础概念及通信过程举例
  2. sftp shell 批量上传文件_Shell自动上传下载文件到SFTP服务器
  3. vba 当前文件名_值得收藏的VBA编程常用代码3640
  4. 查看服务器上读的是哪个配置文件
  5. ElementUI的组件拆解之Tooltip
  6. OTA整包的制作流程(未完)
  7. Java Iterator 接口简介和简单用法.
  8. 向其他进程注入代码的三种方法
  9. php 循环队列,队列和循环队列-php数组
  10. 以太坊开发入门,如何搭建一个区块链DApp投票系统
  11. 计算机电池功能,蓄电池检测仪的主要功能都有哪些
  12. JSP中application的用法
  13. 微信里文件小程序导不出来_微信里的这5款小程序,果然不一般,黑科技啊!...
  14. MarkText常用快捷键
  15. 基于神经网络的图片风格转移小结
  16. MotoSim EG-VRC软件:安川机器人仿真项目基础操作
  17. Git 和Bitbucket
  18. kaggel竞赛之员工离职分析
  19. 对 kubeadm 进行故障排查
  20. Mybatis 任务二:配置文件深入

热门文章

  1. 《黑神话:悟空》将登陆哪些平台 登陆平台介绍
  2. 下列哪个不属于CRF模型对于HMM和MEMM模型的优势( )
  3. 微软与 PageUp
  4. C++ STL-- mt19937
  5. Java案例 | 学籍管理系统(超详解 )
  6. Android 7.0中的多窗口-分屏-实现解析
  7. Lumerical官方案例、FDTD时域有限差分法仿真学习(三)——环形谐振器(Ring resonator)之第一部分
  8. 如何创建你自己的谷歌浏览器扩展
  9. 学习考察清华大学航院天行杯科创成果心得报告
  10. 太极发送卡片软件_软件指标弘历太极信号使用技巧