Maximum Element——stack

Maximum Element
You have an empty sequence, and you will be given queries. Each query is one of these three types:
1 x-Push the element x into the stack.
2-Delete the element present at the top of the stack.
3-Print the maximum element in the stack.
Maximum Element——stack
文章图片

Input Format
The first line of input contains an integer, . The next lines each contain an above mentioned query. (It is guaranteed that each query is valid.)
Constraints Maximum Element——stack
文章图片


Output Format
For each type query, print the maximum element in the stack on a new line.
Sample Input
10
1 97
2
1 20
2
1 26
1 20
【Maximum Element——stack】2
3
1 91
3
Sample Output
26
91
#include
#include
#include
#include
using namespace std;
int main()
{
stackv;
int n;
cin>>n;
while(n--)
{
int ch;
cin>>ch;
if(ch==1)
{
int x;
cin>>x;
v.push(max(x,v.size()>0?v.top():x));
cout< cout<0?v.top():x)< //stack容器的栈顶元素的读取函数为top 函数 将取出最后入栈的元素
//如果没有元素 输入元素
//如果有元素 比较输入元素与原来元素的大小 存大的
}
else if(ch==2)
{
v.pop();
}
else if(ch==3)
{
cout< }
}
return 0;
}

    推荐阅读