蒟蒻求助
查看原帖
蒟蒻求助
855663
0x0001cxr楼主2023/4/23 12:15

只得了20pts,用的栈,调不出来力

#include<bits/stdc++.h>
using namespace std;

string str;
vector<long long> temp;
stack<char> ch;
stack<long long> num;
map<char, int> m;

int main()
{
    cin >> str;
    str += '#';
    m['+'] = 1;
    m['*'] = 2;
    
    for(int i = 0;i < str.size();i++)
    {
        if(str[i] == '0' || str[i] == '1' || str[i] == '2' || 
           str[i] == '3' || str[i] == '4' || str[i] == '5' || 
           str[i] == '6' || str[i] == '7' || str[i] == '8' || str[i] == '9')
           {
               temp.push_back(str[i] - '0');
           }
        else
        {
          if(!temp.empty())
          {
                long long op = 0;
                for(int i = 0;i < temp.size();i++)
                {
                   long long t = pow(10, temp.size() - i - 1);
                   op += t * temp[i];
                }
                num.push(op);
                for(int i = temp.size() - 1;i >= 0;i--)
                {
                    temp.pop_back();
                }
           }
           if(ch.empty())
           ch.push(str[i]);
           else
           {
               while(!ch.empty() && m[ch.top()] >= m[str[i]])
               {
                   char mid = ch.top();
                   ch.pop();
                   long long r = num.top();
                   num.pop();
                   long long l = num.top();
                   num.pop();
                   if(mid == '+')
                   num.push(l + r);
                   if(mid == '*')
                   num.push(l * r);
               }
               ch.push(str[i]);
           }
        }
    }
    
    for(int i = temp.size() - 1;i >= 0;i--)
    {
        temp.pop_back();
    }
    
    long long x = abs(num.top());
    if(x < 10000)
    cout << x << endl;
    else
    {
        int ci = 0;
        while(x > 0)
        {
            ci++;
            temp.push_back(x % 10);
            x /= 10;
            if(ci >= 4)
            break;
        }
        while(temp.size() > 1 && temp.back() == 0)
        temp.pop_back();
        
        for(int i = temp.size() - 1;i >= 0;i--)
        {
            cout << temp[i];
        }
        cout << endl;
    }
    
    return 0;
}
2023/4/23 12:15
加载中...