3139 stack exercises 3, exercises 3

Source: Internet
Author: User

3139 stack exercises 3, exercises 3
3139 stack Exercise 3

 

Time Limit: 2 s space limit: 128000 KB title level: Gold Question View running resultsDescription Description

Compared with the first question, this question adds another operation, accessing the top element of the stack (number 3, to ensure that the stack is not empty when accessing the top element of the stack or when the stack is out ), now the N operation is given, and the output result is.

Input description Input Description

N

N operations (1 in the stack, 2 out of the stack, 3 in the top of the stack)

Output description Output Description

Result of each K row (K is the number of queries in the input)

Sample Input Sample Input

6

1 7

3

2

1 9

1 7

3

Sample output Sample Output

7

7

Data range and prompt Data Size & Hint

For 50% of data, N ≤ 1000 of the inbound stack elements ≤ 200

For 100% of data, N ≤ 100000 of the inbound stack elements are positive integers and smaller than or equal to 10 ^ 4

CATEGORY tag Tags click here to expand
 1 #include<iostream> 2 using namespace std; 3 int stack[100000001]; 4 int top=1; 5 int main() 6 { 7     int n; 8     cin>>n; 9     for(int i=1;i<=n;i++)10     {11         int a;12         cin>>a;13         if(a==2)14         {15             if(top==1)16             {17                 cout<<"impossible!";18                 return 0;19             }20             else21             {22                 top--;23             }24         }25         else if(a==1)26         {27             int b;28             cin>>b;29             stack[top]=b;30             top++;31         }32         else if(a==3)33         {34             cout<<stack[top-1]<<endl;35         }36     }37 /*    if(top==1)38     {39         cout<<"impossible!";40     }41     else42     {43         cout<<stack[top-1];44     }*/45     return 0;46 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.