和题解挺像的但是本地也都是0

P1449 后缀表达式

Xiiii @ 2022-08-12 14:46:28

#include<bits/stdc++.h>
using namespace std;
stack <int> c;
string s;
int main() {
    cin>>s;
    for(int i=0;i<s.length();i++){
        int n=0;
        if(s[i]=='@') break;

        else if(s[i]>='0'&&s[i]<='9'){
            if(n!=0){
                n=n*10+s[i]-48;
            }
            else n=s[i]-48;
        }

        else if(s[i]=='.'){
            c.push(n);
            n=0;
        } 

        else{
            int x=c.top();
            c.pop();
            int y=c.top();
            c.pop();
            if(s[i]=='+') c.push(x+y);
            if(s[i]=='-') c.push(y-x);
            if(s[i]=='*') c.push(x*y);
            if(s[i]=='/') c.push(y/x);
        }

    }

    cout<<c.top();

    return 0;
}

by GuangyuHuashi @ 2022-08-12 14:55:09

同志,你每个循环都把n重置为0了,栈中怎么可能有非0数存进去呢


by GuangyuHuashi @ 2022-08-12 14:56:31

把n的定义放到循环外就可以了


by dcmzwy @ 2022-08-12 14:58:50

Every cycle of your cycle changes n to 0. How can there be non-zero numbers stored in the stack?


by GuangyuHuashi @ 2022-08-12 15:06:46

@Xiiii


by Xiiii @ 2022-08-12 15:22:20

@GuangyuHuashi 谢谢谢谢可能午睡睡傻了()


by Xiiii @ 2022-08-12 15:22:54

@WYTZNB 谢谢


by dcmzwy @ 2022-08-13 13:24:41

@Xiiii Ha ha, I'm glad to help you


|