5 solutions
-
2
2个易错点
1.数据类型要开
unsigned long long
,用int
会爆掉(0<=x<264)2.多组测试数据,每个数据输完要对栈清零
#include<iostream> #include<string> #include<stack> using namespace std; stack<unsigned long long> s; //一定要开unsigned long long!!!!! int t,n; unsigned long long x; string op; int main() { cin>>t; while(t--) { cin>>n; while(n--) { cin>>op; if(op=="push") { cin>>x; s.push(x); } else if(op=="pop") if(s.empty())cout<<"Empty"<<endl; else s.pop(); else if(op=="query") if(s.empty())cout<<"Anguei!"<<endl; else cout<<s.top()<<endl; else if(op=="size") cout<<s.size()<<endl; } while(!s.empty())s.pop();//每个数据输完要清零!!! } }
-
0
1.用string可以不用考虑数据大小 2.一组完后对栈清零
#include<bits/stdc++.h> #include<iostream> #include<stack> using namespace std; stack<string> de; unsigned int T,n,ptr,ptr2; string a,num; int main(){ cin>>T; for(unsigned int i=1;i<=T;i++){ cin>>n; ptr2=ptr; if(ptr>0){ for(unsigned int i=ptr;i>0;i--){ ptr2--; de.pop();} }ptr=ptr2; for(unsigned int j=1;j<=n;j++){ cin>>a; if(a=="push"){ cin>>num; de.push(num); ptr++;} if(a=="pop"){ if(ptr!=0){ de.pop(); ptr--;} else cout<<"Empty"<<endl; } if(a=="query"){ if(ptr==0) cout<<"Anguei!"<<endl; else cout<<de.top()<<endl;} if(a=="size"){ cout<<ptr<<endl; } } } return 0; }
-
0
#include<bits/stdc++.h> using namespace std; #define ull unsigned long long stack<ull> st, st1; ull T, n, x; string s; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> T; while (T--) { cin >> n; while (n--) { cin >> s; if (s == "push") { cin >> x; st.push(x); } if (s == "pop") { if (st.empty()) cout << "Empty" << "\n"; else st.pop(); } if (s == "query") { if (st.empty()) cout << "Anguei!" << "\n"; else cout << st.top() << "\n"; } if (s == "size") cout << st.size() << "\n"; } st = st1; } return 0; }
-
-1
#include<bits/stdc++.h> using namespace std; stack<unsigned long long> line; stack<unsigned long long> line1;//给后面清空 unsigned long long t, n; unsigned long long x; string str; int main() { cin >> t; for(int i = 1; i <= t; i++) { cin >> n; for(int j = 1; j <= n; j++) { cin >> str; if(str=="push") { cin >> x; line.push(x); } else if(str=="pop") { if(line.empty()) { cout<<"Empty"<<endl; } else { line.pop(); } } else if(str=="query") { if(line.empty()) { cout<<"Anguei!"<<endl; } else { cout<<line.top()<<endl; } } else if(str=="size") { cout<<line.size()<<endl; } } line = line1; } return 0; }
-
-2
回来发个题解[乐]
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; int main(int argc, char **argv){ int t; cin >> t; while (t--){ stack<ull> a; int n; cin >> n; while (n--){ string op; cin >> op; if (op == "push"){ ull x; scanf("%llu",&x); a.push(x); } else if (op == "pop") { if (a.empty()) printf("Empty\n"); else a.pop(); }else if (op == "query"){ if (a.empty()) printf("Anguei!\n"); else printf("%llu\n",a.top()); }else{ printf("%ld\n",a.size()); } } } }
- 1
Information
- ID
- 1114
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 8
- Tags
- # Submissions
- 144
- Accepted
- 22
- Uploaded By