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 }