样例没过,求助

P1449 后缀表达式

yzm0325 @ 2023-08-11 14:58:16

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

by ZZX_6666 @ 2023-08-11 15:12:15

把x-y换成y-x即可


by ZZX_6666 @ 2023-08-11 15:12:37

@zym0325


by jasonshen_ @ 2023-08-11 15:27:32


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

@zym0325


by yzm0325 @ 2023-08-11 15:52:43

@ZZX_6666 thx


|