PAT堆栈模拟队列
2016-07-12 08:58
211 查看
使用堆栈模拟队列,相当于当用两个桶装水:
一:为A 装水的时候
当左边的水桶满了,而右边的不为空的话,就可以说不能倒水了,输出Full
当左边的水桶没满,就可以倒到它满为止
当左边的水桶满了,右边的没有水,就把所有的水倒到右边去
二:倒水的时候
当右边的水桶满了之后就可以倒
如果是空的重新从左边装水
如果左边也没有水,
那么说明是空的,输出Empty
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
stack<int>a1;
stack<int>a2;
int main(){
int n1,n2;
cin>>n1>>n2;
if(n1 > n2){
swap(n1,n2);
}
int dex;
char c;
while(cin>>c && c != 'T'){
if(c == 'A'){
cin>>dex;
if(a1.size() == n1 &&a2.size() != 0){
cout<<"ERROR:Full"<<endl;
continue;
}
if(a1.size() != n1){
a1.push(dex);
}else{
while(a1.size() != 0){
a2.push(a1.top());
a1.pop();
}
a1.push(dex);
}
}
if(c == 'D'){
if(a2.size() == 0 && a1.size() == 0){
cout<<"ERROR:Empty"<<endl;
continue;
}
if(a2.size()== 0 && a1.size() != 0){
while(a1.size() != 0){
a2.push(a1.top());
a1.pop();
}
cout<<a2.top()<<endl;
a2.pop();
}else{
cout<<a2.top()<<endl;
a2.pop();
}
}
}
return 0;
}
一:为A 装水的时候
当左边的水桶满了,而右边的不为空的话,就可以说不能倒水了,输出Full
当左边的水桶没满,就可以倒到它满为止
当左边的水桶满了,右边的没有水,就把所有的水倒到右边去
二:倒水的时候
当右边的水桶满了之后就可以倒
如果是空的重新从左边装水
如果左边也没有水,
那么说明是空的,输出Empty
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
stack<int>a1;
stack<int>a2;
int main(){
int n1,n2;
cin>>n1>>n2;
if(n1 > n2){
swap(n1,n2);
}
int dex;
char c;
while(cin>>c && c != 'T'){
if(c == 'A'){
cin>>dex;
if(a1.size() == n1 &&a2.size() != 0){
cout<<"ERROR:Full"<<endl;
continue;
}
if(a1.size() != n1){
a1.push(dex);
}else{
while(a1.size() != 0){
a2.push(a1.top());
a1.pop();
}
a1.push(dex);
}
}
if(c == 'D'){
if(a2.size() == 0 && a1.size() == 0){
cout<<"ERROR:Empty"<<endl;
continue;
}
if(a2.size()== 0 && a1.size() != 0){
while(a1.size() != 0){
a2.push(a1.top());
a1.pop();
}
cout<<a2.top()<<endl;
a2.pop();
}else{
cout<<a2.top()<<endl;
a2.pop();
}
}
}
return 0;
}
相关文章推荐
- libdvbpsi源码分析(四)PAT表解析/重建
- PAT配置
- 什么是端口复用动态地址转换(PAT) 介绍配置实例
- MikroTik layer7-protocol
- PAT是如何工作的
- PAT 乙级题:1002. 写出这个数 (20)
- PAT (Advanced Level) Practise 1001-1010
- 数据结构学习与实验指导(一)
- PAT Basic Level 1001-1010解题报告
- PAT 数素数
- PAT 福尔摩斯的约会
- PAT 德才论
- PAT 月饼
- 1001. 害死人不偿命的(3n+1)猜想
- 1002. 写出这个数
- 1032. 挖掘机技术哪家强
- 1001. 害死人不偿命的(3n+1)猜想 (PAT basic)
- 1002. 写出这个数(PAT Basic)
- 1004. 成绩排名(PAT Basic)
- 1006. 换个格式输出整数(PAT Basic)