66pts求条

B3614 【模板】栈

qiuyongqin @ 2024-10-24 20:55:12


#include<bits/stdc++.h>
using namespace std;

int main()
{
    std::ios::sync_with_stdio( false );
    std::cin.tie( 0 ), std::cout.tie( 0 );
    unsigned long long t, n;
    std::cin >> t;
    for( int i = 1; i <= t; i++ )
    {
        std::cin >> n;
        for( int j = 1; j <= n; j++ )
        {

            string str;
            std::cin >> str;
            std::stack<unsigned long long> s;
            if( str[0] == 'p' && str[1] == 'u' )
            {
                unsigned long long x;
                std::cin >> x;
                s.push( x );
            }
            else if( str[0] == 'p' && str[1] == 'o' )
            {
                if( s.empty() ) std::cout << "Empty\n";
                else s.pop(); 
            }
            else if( str[0] == 'q' )
            {
                if( s.empty() ) std::cout << "Anguei!\n";
                else std::cout << s.top() << "\n";
            }
            else
            {
                std::cout << s.size() << "\n";
            }
        }
    }
    return 0; 
}

by qiuyongqin @ 2024-10-24 20:57:10

最后一个点WA!!!


by yiziqiao @ 2024-10-24 20:58:59

@qiuyongqin


by yiziqiao @ 2024-10-24 21:04:08

#include<bits/stdc++.h>
#define ull unsigned long long
using namespace std;
const int N=1e6+10;

int n,t,top;
ull sta[N],x;
string op;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0); 
    cin>>t;
    while(t--){
        top=0;
        cin>>n;
        while(n--){
            cin>>op;
            if(op=="push"){
                cin>>x;
                sta[++top]=x;
            }else if(op=="pop"){
                if(top) --top;
                else cout<<"Empty\n";
            }else if(op=="query"){
                if(top) cout<<sta[top]<<"\n";
                else cout<<"Anguei!\n"; 
            }else if(op=="size") cout<<top<<"\n";
        }
    } 
    return 0;
} 

by yiziqiao @ 2024-10-24 21:04:53

求关


|