样例算出来是-48......

P1449 后缀表达式

HiAI @ 2022-08-09 11:12:19

#include<bits/stdc++.h>
using namespace std;
char a[200005];
int cnt,s;
int main(){
    while(a[cnt]!='@'){
        cnt++;
        cin>>a[cnt];
        if(a[cnt]=='+'){
            cnt-=4;
            s=int(a[cnt])+int(a[cnt+2]);
            a[cnt]=char(s);
            cnt++;
        }
        if(a[cnt]=='-'){
            cnt-=4;
            s=int(a[cnt])-int(a[cnt+2]);
            a[cnt]=char(s);
            cnt++;
        }
        if(a[cnt]=='*'){
            cnt-=4;
            s=int(a[cnt])*int(a[cnt+2]);
            a[cnt]=char(s);
            cnt++;
        }
        if(a[cnt]=='/'){
            cnt-=4;
            s=int(a[cnt])/int(a[cnt+2]);
            a[cnt]=char(s);
            cnt++;
        }
    }
    cout<<s;
    return 0;
} 

by Elly_1412 @ 2022-08-09 11:16:58

#include<bits/stdc++.h>
using namespace std;
long long s[1000];
int main() {
    long long i = 0, t = 0;
    char a;
    while ((a = getchar()) != '@') {
        if (a >= '0' && a <= '9') {
            t *= 10;
            t += a - '0';
        } else if (a == '.') {
            s[++i] = t;
            t = 0;
        } else if (a == '+') {
            s[i - 1] = s[i - 1] + s[i];
            s[i] = 0;
            i--;
        } else if (a == '-') {
            s[i - 1] = s[i - 1] - s[i];
            s[i] = 0;
            i--;
        } else if (a == '*') {
            s[i - 1] = s[i - 1] * s[i];
            s[i] = 0;
            i--;
        } else if (a == '/') {
            s[i - 1] = s[i - 1] / s[i];
            s[i] = 0;
            i--;
        }
    }
    cout << s[1] << " ";
    return 0;
}

对比一下?


by HiAI @ 2022-08-09 11:52:26

@1班张心悦 为什么t一定要乘10


by Elly_1412 @ 2022-08-09 11:56:04

@HiAI 这个操作是把输入的char类型字符转换到t种储存,t乘以10之后就多了一位,就是原来字符里a的一位

很早之前写的题了,我记得大概就是这样的


by HiAI @ 2022-08-09 11:58:17

@1班张心悦 谢谢


|