过了样例,1、3、5wa,2、4、6re

P1449 后缀表达式

aishiteru_mitsu_ha @ 2024-03-23 21:51:51

#include<iostream>
#include<cstring>
using namespace std;
char stack[1000010];
long long len_s, num[1000010], top;
int main() {
    cin >> stack;
    len_s = strlen(stack);
    for (int i = 0; i < len_s; i++) {
        if (stack[i] == '@') break;
        if (stack[i] == '.') continue;
        if (stack[i] - '0' >= 0 && stack[i] - '0' <= 9) {
            if (i - 1 >= 0) {
                if (stack[i - 1] != '.' && stack[i - 1] != '+' && stack[i - 1] != '-' && stack[i - 1] != '*' && stack[i - 1] != '/' && stack[i - 1] != '%') {
                    num[top] = num[top] * 10 + (stack[i] - '0');
                }
                else {
                    num[++top] = stack[i] - '0';
                }
            }
            else {
                num[++top] = stack[i] - '0';
            }
        }
        else {
            switch (stack[i]) {
                case '+':
                    num[--top] = num[top] + num[top + 1];
                    break;
                case '-':
                    num[--top] = num[top] - num[top + 1];
                    break;
                case '*':
                    num[--top] = num[top] * num[top + 1];
                    break;
                case '/':
                    num[--top] = num[top] / num[top + 1];
                    break;
                case '%':
                    num[--top] = num[top] % num[top + 1];
                    break;
            }
        }
    }
    cout << num[1] << endl;
    return 0;
}

by aishiteru_mitsu_ha @ 2024-04-06 18:25:45

已解决,把num[--top]里的--top移出来单独执行就行了


|