全TLE求助

P1449 后缀表达式

wangzaixi @ 2024-08-07 22:22:18

#include<bits/stdc++.h>
using namespace std;
stack <int> s1;
int main(){
    char a;
    int b,x,y;
    while(a!='@'){
        b=((int)a)-48;
        if(a>='0'&&a<='9'){
            s1.push(b);
        }
        else if(a=='+'){
            x=s1.top();
            s1.pop();
            y=s1.top();
            s1.pop();
            s1.push(x+y);
        }
        else if(a=='-'){
            x=s1.top();
            s1.pop();
            y=s1.top();
            s1.pop();
            s1.push(x-y);
        }
        else if(a=='*'){
            x=s1.top();
            s1.pop();
            y=s1.top();
            s1.pop();
            s1.push(x*y);
        }
        else if(a=='/'){
            x=s1.top();
            s1.pop();
            y=s1.top();
            s1.pop();
            s1.push(x/y);
        }
    }
    cout<<s1.top();
    return 0;
}

by haimingbei @ 2024-08-07 22:32:19

@wangzaixi

(AC,求关注)
/*
3、P1449 后缀表达式
*/
#include<bits/stdc++.h>
using namespace std;
stack <int> st;
int main(){
    char c;
    int n=0;
    cin>>c;
    while(c!='@'){
        if(c>='0' && c<='9')    n=n*10+c-'0';
        else if(c=='.') st.push(n),n=0;
        else {
            int x,y,t;
            x=st.top();
            st.pop();
            y=st.top();
            st.pop();
            if(c=='+')t=x+y;
            else if(c=='-')t=y-x;
            else if(c=='*')t=x*y;
            else if(c=='/')t=y/x;
            st.push(t);
        }
        cin>>c;
    }
    cout<<st.top();
    return 0;
}

by wangzaixi @ 2024-08-07 23:53:55

@haimingbei n是啥


by haimingbei @ 2024-08-08 09:52:17

@wangzaixi 计算数字


by wangzaixi @ 2024-08-08 19:56:10

@haimingbei 计算数字是啥


by haimingbei @ 2024-08-08 20:02:43

@wangzaixi ……因为它是字符串(这种计算手法不知道吗)


by wangzaixi @ 2024-08-09 14:00:45

@haimingbei MLE了


#include<bits/stdc++.h>
using namespace std;
stack <int> s1;
char a;
int b,x,y;
int main(){
cin>>a;
    while(a!='@'){
        b=((int)a)-48;
        if(a>='0'&&a<='9'){
            s1.push(b);
        }
        else{
            x=s1.top();
            s1.pop();
            y=s1.top();
            s1.pop();
            if(a=='+'){
                s1.push(x+y);
            }
            else if(a=='-'){
                s1.push(x-y);
            }
            else if(a=='*'){
                s1.push(x*y);
            }
            else if(a=='/'){
                s1.push(x/y);
            }
        }
    }
    cout<<s1.top();
    return 0;
}

by haimingbei @ 2024-08-09 14:11:06

@wangzaixi 你自己再看看我的代码吧,你抄代码最基本的也能抄漏掉,你6


by haimingbei @ 2024-08-09 14:11:29

@wangzaixi 自己找不同吧

#include<bits/stdc++.h>
using namespace std;
stack <int> st;
int main(){
    char c;
    int n=0;
    cin>>c;
    while(c!='@'){
        if(c>='0' && c<='9')    n=n*10+c-'0';
        else if(c=='.') st.push(n),n=0;
        else {
            int x,y,t;
            x=st.top();
            st.pop();
            y=st.top();
            st.pop();
            if(c=='+')t=x+y;
            else if(c=='-')t=y-x;
            else if(c=='*')t=x*y;
            else if(c=='/')t=y/x;
            st.push(t);
        }
        cin>>c;
    }
    cout<<st.top();
    return 0;
}

by wangzaixi @ 2024-08-09 14:14:13

@haimingbei 我不是抄代码...


by haimingbei @ 2024-08-09 14:14:23

@wangzaixi 首先,你30行和31行之间再输入a去哪里?


| 下一页