全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 wangzaixi @ 2024-08-09 14:39:22

@haimingbei 谢谢 献上AC代码

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

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

@wangzaixi 给个关注呗,俺这么认真!!!


by mairuisheng @ 2024-08-09 18:57:27

@wangzaixi

修改如下(注释在里面):

#include<bits/stdc++.h>
using namespace std;
stack <long long> s1;//题目范围 :10^9,要开long long 
int main(){
    char a;
    long long b,x,y,slowsum=0;//局内变量必须清零 
    do//改为do-while 
    {
        a=getchar();//哥们,你输入呢? 
        if(a=='@')break;//判断是否结束 
        b=((int)a)-48;
        if(a>='0'&&a<='9'){
            //举个例子:如果读入的为  5678
            // 那么这段代码的操作是:0*10=0,0+5=5 -----> 5*10=50,50+6=56 ----->
            // 56*10=560,560+7=567 -----> 567*10=5670,5670+8=5678 -----> 结束。 
            slowsum*=10;
            slowsum+=b;
        }
        else if(a=='.'){
            s1.push(slowsum);//遇到点就把已获取数字入栈,其实题目表意不清楚 
            slowsum=0;//slowsum清零 ,不然下次使用会在原来的数字基础上增加 
        }
        else if(a=='+'){
            x=s1.top();
            s1.pop();
            y=s1.top();
            s1.pop();
            s1.push((y+x));
        }
        else if(a=='-'){
            x=s1.top();
            s1.pop();
            y=s1.top();
            s1.pop();
            s1.push((y-x));//栈是先进后出,所以是y-x 
        }
        else if(a=='*'){
            x=s1.top();
            s1.pop();
            y=s1.top();
            s1.pop();
            s1.push((y*x));
        }
        else if(a=='/'){
            x=s1.top();
            s1.pop();
            y=s1.top();
            s1.pop();
            s1.push((y/x));//栈是先进后出,所以是y/x 
        }
    }while(a!='@');
    cout<<s1.top();
    return 0;
}

或者,参考我的代码:

#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
typedef long long LL;
stack<LL> s;
char a[51];
LL slowsum,c1,c2,i;
int main()
{
    gets(a);
    for(i=0;i<strlen(a);i++)
    {
        if(a[i]=='@')break;
        if(a[i]=='.')
        {
            s.push(slowsum);
            slowsum=0;
        }
        else if(a[i]>=48&&a[i]<=57)
        {
            slowsum*=10;
            slowsum=slowsum+(a[i]-48);
        }
        else
        {
            c1=s.top();
            s.pop();
            c2=s.top();
            s.pop();
            switch(a[i])
            {
                case '+':s.push((c2+c1));break;
                case '-':s.push((c2-c1));break;
                case '*':s.push((c2*c1));break;
                case '/':s.push((c2/c1));break;
            }
        }
    }
    printf("%lld",s.top());
    return 0;
}

by liumoumou_haha @ 2024-08-20 09:09:44

@wangzaixi AC代码

#include<bits/stdc++.h>
using namespace std;
string a;
stack<int> st;
int num,num2,num1;
int main()
{
    cin>>a;
    for(int i=0;i<a.size();i++){
        if(a[i]>='0'&&a[i]<='9'){
            num=num*10+a[i]-'0';
        }
        else if(a[i]=='.'){
            st.push(num);
            num=0;
        }
        else if(a[i]=='-'){
            num1=st.top();
            st.pop();
            num2=st.top();
            st.pop();
            st.push(num2-num1);
        } 
        else if(a[i]=='+'){
            num1=st.top();
            st.pop();
            num2=st.top();
            st.pop();
            st.push(num1+num2);
        }
        else if(a[i]=='*'){
            num1=st.top();
            st.pop();
            num2=st.top();
            st.pop();
            st.push(num1*num2);
        }
        else if(a[i]=='/'){
            num1=st.top();
            st.pop();
            num2=st.top();
            st.pop();
            st.push(num2/num1);

        }
    }
    cout<<st.top();
    return 0;
} 

上一页 |