哼哼啊啊啊啊啊 5TLE 5RE 救救 stack

P1449 后缀表达式

LubsWangKillThemAll @ 2022-05-30 18:27:45

#include<iostream>
#include<stdio.h>
#include<stack>
using namespace std;
stack<int>s;
int main()
{
    char c;
    int num = 0;
    while (1)
    {
        cin >> c;
        if (c >= '0' && c >= '9')
        {
            num = num * 10 + c - '0';
        }
        else if (c == '.')
        {
            s.push(num);
            num = 0;
        }
        else if (c == '+')
        {
            int num1 = s.top();
            s.pop();
            int num2 = s.top();
            s.pop();
            s.push(num1 + num2);
        }
        else if (c == '-')
        {
            int num1 = s.top();
            s.pop();
            int num2 = s.top();
            s.pop();
            s.push(num2 - num1);
        }
        else if (c == '*')
        {
            int num1 = s.top();
            s.pop();
            int num2 = s.top();
            s.pop();
            s.push(num1 * num2);
        }
        else if (c == '/')
        {
            int num1 = s.top();
            s.pop();
            int num2 = s.top();
            s.pop();
            s.push(num2 / num1);
        }
        else if (c == '@')
        {
            break;
        }
    }
    cout << s.top();
}

by LubsWangKillThemAll @ 2022-05-30 18:28:30

啊说错了 3TLE 3RE


by Xy_top @ 2022-05-30 18:29:24

何必写的这么麻烦呢?


by Xy_top @ 2022-05-30 18:29:39

#include <bits/stdc++.h>
using namespace std;
stack<int> n;
char ch;
int s,x,y;
int main()
{
    while(ch!='@')
    {
        ch=getchar();
        switch(ch)
        {
            case '+':x=n.top();n.pop();y=n.top();n.pop();n.push(x+y);break;
            case '-':x=n.top();n.pop();y=n.top();n.pop();n.push(y-x);break;
            case '*':x=n.top();n.pop();y=n.top();n.pop();n.push(x*y);break;
            case '/':x=n.top();n.pop();y=n.top();n.pop();n.push(y/x);break;
            case '.':n.push(s);s=0;break;
            default :s=s*10+ch-'0';break;
        }
    }
    printf("%d\n",n.top());
    return 0;
}

by Xy_top @ 2022-05-30 18:30:03

看看是不是结构更清晰点,更容易理解点


by LubsWangKillThemAll @ 2022-05-30 18:30:22

@stdios 哦哦哦


by _HiKou_ @ 2022-05-30 18:54:07

好臭啊(


by LubsWangKillThemAll @ 2022-06-01 08:49:28

@Hi_Kou 都发霉了


by Come_wonka @ 2022-08-10 09:48:31

@stdios 谢谢大佬,我们菜鸡只熟练最开始的多重if


|