3138 stack Exercises 2, exercises 2

Source: Internet
Author: User

3138 stack Exercises 2, exercises 2
3138 stack Exercise 2

 

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

(Compared with Stack Exercise 1, this question is changed to two places: 1. Enhanced Data. 2. There is no guarantee that the stack will not go out of the stack when the stack is empty)

Given a stack (initially empty, element type is integer, and smaller than or equal to 100), there are only two operations: inbound and outbound. First, these operations are provided. Please output the stack top element of the final stack. Operation explanation: 1 indicates inbound stack, 2 indicates outbound Stack

Input description Input Description

N (number of operations)

N operations (if it is an inbound stack, there will be an inbound stack element)

For details, see the example (the input does not guarantee that the stack will not go out of the stack when the stack is empty)

Output description Output Description

The final stack top element outputs "impossible!" If the final stack is empty or the stack is empty !" (Without quotation marks)

Sample Input Sample Input

3

1 2

2

2

Sample output Sample Output

Impossible!

Data range and prompt Data Size & Hint

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

 

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         else26         {27             int b;28             cin>>b;29             stack[top]=b;30             top++;31         }32     }33     if(top==1)34     {35         cout<<"impossible!";36     }37     else38     {39         cout<<stack[top-1];40     }41     return 0;42 }

 

Related Article

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.