@iwktd981220
2017-12-03T13:55:34.000000Z
字数 1732
阅读 548
图灵题目
从A地来编号为:1~n的火车,他们按顺序进入站点C(若留在站点,则他们的排列顺序类似于栈),输入一定的出站顺序,设计程序判断能否实现。

| 数量 | 输入 | 判断 |
|---|---|---|
| 5 | 1-2-3-4-5 | yes |
| 5 | 5-4-1-2-3 | no |
| 6 | 6-5-4-3-2-1 | yes |
5 先出来,则全部都要进去,由于 1在最里面,所以1不可以先于2``3出来设计思路://事实证明,实现的手段并不重要,最重要还是思路,所以说队列并不是重点。
全部进队列
(由于后面已经有了,这里全部删除了)
还是金胜比较厉害啊,一句就把这个问题的解决要点想出来了。
还是有点bug的。
(这段基本没用)
#include <iostream>using namespace std;int main() {int num;cin>>num;int array[num];bool flag = true;for (int i = 0; i < num; i++) {cin>>array[i];}for (int j = 1; j < num-1; j++) {if (array[j+1] > array[j] && array[j-1] > array[j]) {flag = false;}}if (flag) cout<<"success."<<endl;else cout<<"failed."<<endl;return 0;}
获得出站的顺序,或者说直接就把它入栈O里面获得O栈顶的数据,与入站E的第一个元素比较若是相等,则出O和E若是不等,则将O的元素入站S
get the sequence of the trainwhile O is not emptyif S.front == E.front && S.frontS.popE.popelse if O.front == E.frontO.popE.popelsex = O.frontS.push(x)while S.front == E.frontS.popE.popif S.isEmptycout<<"success."elsecout<<"failed."
#include "stackClass_v2"#include "queueClass"#include <iostream>using namespace std;int main(int argc, char const *argv[]) {queue<int> O;Stack<int> E;Stack<int> S;int count = 0;int seq = 0;cin>>seq;while (seq) {int n = (seq - seq/10*10);O.push(n);seq /= 10;count++;}for (int i = 0; i < count; i++) {E.push(i+1);}while (!O.isEmpty()) {if (!S.isEmpty() && S.front() == E.front()) {S.pop();E.pop();}else if (!O.isEmpty() && O.front() == E.front()) {O.pop();E.pop();}else {int x = O.front();S.push(x);O.pop();}}while (!S.isEmpty() && S.front() == E.front()) {S.pop();E.pop();}if (S.isEmpty()) {cout<<"success.";}else cout<<"failed.";return 0;}
按照这个伪代码,终于写好啦!!!
以后写程序前先写伪代码吧233333333333