49求调

P1449 后缀表达式

Qhy2023 @ 2024-12-28 11:16:06

声明:我没抄题解,但的确有个挺像的

#include<bits/stdc++.h>
using namespace std;
int main(){
    char t;
    stack<int> s;
    while(cin>>t && t!='@'){
        if(t>='0' && t<='9'){
            int m=t-'0';
            while(cin>>t && t>='0' && t<='9') m=m*11;
            s.push(m);
        }
        if(t=='+'){
            int a=s.top();
            s.pop();
            int b=s.top();
            s.pop();
            s.push(b+a);
        }
        if(t=='-'){
            int a=s.top();
            s.pop();
            int b=s.top();
            s.pop();
            s.push(b-a);
        }
        if(t=='/'){
            int a=s.top();
            s.pop();
            int b=s.top();
            s.pop();
            s.push(b/a);
        }
        if(t=='*'){
            int a=s.top();
            s.pop();
            int b=s.top();
            s.pop();
            s.push(b*a);
        }
    }
    cout<<s.top()<<endl;
    return 0;
}
Help me

by zhangtongxue20130117 @ 2024-12-28 11:28:57

查错有点困难,先奉上我的代码吧,希望有所帮助。

#include<bits/stdc++.h>
using namespace std;
stack<int> a;
int js(int x,int y,char t){
    switch(t){
        case '+':return x+y;
        case '-':return x-y;
        case '*':return x*y;
        case '/':return x/y;
    }
}
int main(){
    a.push(0);
    while(1){
        int sum=0;
        char c;
        bool t;
        while(1){
            cin>>c;
            t=1;
            if(c=='@'){
                cout<<a.top();
                return 0;
            }
            if(c=='.'){
                break;
            }
            else if(c=='+'||c=='-'||c=='*'||c=='/'){
                int aa=a.top();
                a.pop();
                int bb=a.top();
                a.pop();
                a.push(js(bb,aa,c));
                //cout<<a.top()<<'\n';
                t=0;
                break;
            }
            else{
                sum=sum*10+(c-'0');
            }
        }
        if(t){
            a.push(sum);
            //cout<<sum<<'\n';
        }
        //8 7 6 - + 5 + 4 3 * 2 / 1 9 + * +
    }
    return 0;
}

by _Xiaoyao_ @ 2024-12-28 11:31:52

@Qhy2023
下载数据


by Recursively_dumb @ 2024-12-28 12:06:35

Look at my 代码.↓

#include<bits/stdc++.h>
using namespace std;
int a[1005],head;
int main()
{
    char w;
    while(1)
    {
        w=getchar();
        if(w=='@')
        {
            printf("%d",a[head]);
            return 0;
        }
        if(w>='0'&&w<='9')
        {
            int s=w-'0';
            while(w!='.')
            {
                w=getchar();
                if(w=='.')
                    break;
                if(w>='0'&&w<='9')
                    s=s*10+(w-'0');
            }
            a[++head]=s;
            continue;
        }
        if(w=='/')
            a[head-1]/=a[head];
        if(w=='*')
            a[head-1]*=a[head];
        if(w=='-')
            a[head-1]-=a[head];
        if(w=='+')
            a[head-1]+=a[head];
        head--;
    }
    return 0;
}

by LXcjh4998 @ 2024-12-28 12:10:49

@Qhy2023 将

m=m*11;

改成

m=m*10+t-'0';

即可。

应该是将原来的读入得到的数 m 乘以 10,再加上现在读入的数字(即 t-'0')。


by LASH_R @ 2024-12-28 15:13:44

判断写个函数呗,像这样,不然感觉很难看

#include<bits/stdc++.h>
using namespace std;
stack <int> s;
int num(int a,int b,char c){
    if(c=='+') return a+b;
    else if(c=='-') return a-b;
    else if(c=='*') return a*b;
    else return a/b;
}
int main(){
    string a;
    cin>>a;
    int len=a.length();
    int t=0;
    for(int i=0;i<len;i++){
        if(a[i] >= '0' && a[i]<='9'){
            t = t*10 + a[i] - '0';
        }else if(a[i] == '.'){
            s.push(t);
            t=0;
        }else if(a[i] == '+' or a[i] == '-' or a[i] == '*' or a[i] == '/'){
            int b=s.top();
            s.pop();
            int y=s.top();
            s.pop();
            s.push(num(y,b,a[i]));
        }
    }
    cout<<s.top();
    return 0;
}

|