3137 stack Exercises 1, 3 seconds Exercises 1

Source: Internet
Author: User

3137 stack Exercises 1, 3 seconds Exercises 1
3137 stack Exercise 1

 

Time Limit: 1 s space limit: 128000 KB title level: Gold QuestionDescription Description

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 sample (input ensures that the stack will not go out when the stack is empty)

Output description Output Description

The final top element of the stack. If the final stack is empty, "impossible!" Is output !" (Without quotation marks)

Sample Input Sample Input

3

1 2

1 9

2

Sample output Sample Output

2

Data range and prompt Data Size & Hint

For 100% of data, elements N ≤ 1000 are positive integers and less than or equal to 100

 1 #include<cstdio> 2 #include<cstdlib> 3 #include<string> 4 #include<cstring> 5 #include<iostream> 6 using namespace std; 7 int a[10000]; 8 int b[10000]; 9 int st[10000];10 int main()11 {12     int n;13     cin>>n;14     for(int i=1;i<=n;i++)15      {16          cin>>a[i];17          if(a[i]==1)18           {19               cin>>b[i];20           }21       } 22 23      int top=0;24      for(int i=1;i<=n;i++)25       {26           if(a[i]==1)27            {28                top++;29                st[top]=b[i];30            }31           if(a[i]==2)32            {33                top--;34             } 35        } 36        if(top<=0)37         {38             cout<<"impossible!";39             return 0;40         }41          else cout<<st[top]; 42       43       return 0;44 }

 

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.