3 条题解

  • 3
    @ 2024-2-24 14:32:02

    不想多思考,表达式的值代码如下,包教包会,一个赞拿走

    #include <bits/stdc++.h>
    #define ll long long
    using namespace std;
    stack<char> s1;
    stack<int> s, s2;
    char now;
    int x, y; 
    void work(string c)
    {
    	for (int i = 0; i < c.length(); i++)
    	{
            if (c[i] >= '0' && c[i] <= '9') 
    		{
    			now *= 10;
    			now += c[i] - '0';
    		}	
            else if (c[i] == '.')
    		{
            	s.push(now);
            	now = 0;
            }
            else if (c[i] == '+')
    		{
            	x = s.top();
            	s.pop();
            	y = s.top();
            	s.pop();
            	s.push(y + x);
            }
            else if (c[i] == '-')
    		{
    		    x = s.top();
    		    s.pop();
    		    y = s.top();
    		    s.pop();
    		    s.push(y - x);
            }
            else if (c[i] == '*')
    		{
    		    x = s.top();
    		    s.pop();
    		    y = s.top();
    		    s.pop();
    		    s.push(y * x);
            }
            else if (c[i] == '/')
    		{
    		    x = s.top();
    		    s.pop();
    		    y = s.top();
    		    s.pop();
    		    s.push(y / x);
            }
        }
    }
    int main()
    {
    	int now = 0;
        string c;
        cin >> c;
        for (int i = 0; i < c.length(); i++)
    	{
            if (c[i] >= '0' && c[i] <= '9')
    		{
    			s2.push(c[i]);
    			if (!(c[i + 1] >= '0' && c[i + 1] <= '9'))
    				s2.push('.');	   
    		} 
            else if (c[i] == '+' || c[i] == '-')
            {
        		while (!s1.empty() && s1.top() != '(')
        		{
        			s2.push(s1.top());
        			s1.pop();
    			}
    			s1.push(c[i]);
    		}
            else if (c[i] == '*' || c[i] == '/')
            {
        		while (!s1.empty() && (s1.top() == '*' || s1.top() == '/'))
        		{
        		    s2.push(s1.top());
        		    s1.pop();
    			}
                s1.push(c[i]);
            }
            else if (c[i] == '(')
    			s1.push(c[i]);
            else if (c[i] == ')')
            {
    			while (s1.top() != '(')
        		{
        			s2.push(s1.top());
        			s1.pop();
    			}
    			s1.pop();
            }
        }
        while(!s1.empty())
        {
        	s2.push(s1.top());
        	s1.pop();
    	}
    	while(!s2.empty())
        {
        	s1.push(s2.top());
        	s2.pop();
    	}
    	string cx = "";
    	while(!s1.empty())
    	{
    		cx += s1.top();
    		s1.pop();
    	}
    	work(cx);
    	cout << s.top() << endl;
        return 0;
    }
    
    • @ 2024-3-9 11:34:12

      这是有屎以来最长的ACAC代码了

  • 1
    @ 2024-2-17 17:32:42
    #include<iostream>
    using namespace std;
    int a,b,c;
    char x;
    int main(){
        cin>>x>>a>>x>>b>>x>>x>>c;
        cout<<(a+b)*c;
        return 0;
    
    • 1
      @ 2023-10-5 15:47:29

      坏事是用long long会溢出,所以要用高精度算法;好事是答案只有一个 所以上代码:

      #include <iostream>
      using namespace std;
      int main(){
          cout<<408<<endl;
          return 0;
      }
      
      • 1

      信息

      ID
      322
      时间
      1000ms
      内存
      16MiB
      难度
      1
      标签
      递交数
      39
      已通过
      29
      上传者