救济孩子吧!只过了一个用例,用了stack,本地用例能过

P1449 后缀表达式

syy981201 @ 2022-03-16 17:28:41

#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
const int N=1010;
int main()
{
    int now;
    char a;
    stack<int> s;
    while((a=getchar())!='@')
    {
        if(a>='0'&&a<='9') 
        {
            now*=10;
            now+=a-'0';
        }
        else if(a=='.')
        {
            s.push(now);
            now=0;
        }
        else if(a=='+')
        {
            int y=s.top();s.pop();
            int x=s.top();s.pop();
            int z=x+y;
            s.push(z);
        }
        else if(a=='-')
        {
            int y=s.top();s.pop();
            int x=s.top();s.pop();
            int z=x-y;
            s.push(z);
        }
        else if(a=='*')
        {
            int y=s.top();s.pop();
            int x=s.top();s.pop();
            int z=x*y;
            s.push(z);
        }
        else if(a=='/')
        {
            int y=s.top();s.pop();
            int x=s.top();s.pop();
            int z=x/y;
            s.push(z);
        }
    }
    cout<<s.top()<<endl;
    return 0;
}

by Dream_weavers @ 2022-03-16 18:30:54

看看把int改成longlong能不能过


by loip @ 2022-03-20 12:36:44

第七行int now; 把now初值赋为0即可int now=0;


|