1 条题解

  • 2
    @ 2023-11-27 14:49:38

    模仿课上后缀表达式的解法,然后根据题目的运算规则写出来即可

    核心代码
    
    for(int i=c.length()-1;i>=0;i--)
    	{
            if(c[i]>='0'&&c[i]<='9') 
    			s.push(c[i]-'0');
            else if(c[i]=='+'){
            	x=s.top();
            	s.pop();
            	y=s.top();
            	s.pop();
            	s.push(x+y);
            }
            else if(c[i]=='-'){
    			x=s.top();
            	s.pop();
            	y=s.top();
            	s.pop();
            	s.push(x-y);
            }
            else if(c[i]=='*'){
    			x=s.top();
            	s.pop();
            	y=s.top();
            	s.pop();
            	s.push(x*y);
            }
            else if(c[i]=='/'){
    			x=s.top();
            	s.pop();
            	y=s.top();
            	s.pop();
            	s.push(x/y);
            }
        }
    
    • @ 2024-2-24 14:48:06

      帮易云老师补充以下代码,只不过做了亿点点改动而已

      #include <bits/stdc++.h>
      #define ll long long
      #define zhan stack
      #define zifuchuan string
      #define zhengshu int
      #define shuru cin
      #define shuchu cout
      #define yaru push
      #define xunhuan for
      #define ruguo if
      #define fouze else
      #define huanhang "\n"
      #define tanchu pop
      #define shiyong using
      #define mingmingkongjian namespace
      #define head top
      #define fanhui return
      #define xiaoyv <<
      #define dayv >>
      #define zhuhanshu main
      shiyong mingmingkongjian std;
      zhan<zhengshu> s;
      zifuchuan c;
      zhengshu x, y;
      zhengshu zhuhanshu()
      {
          shuru dayv c;
          xunhuan (zhengshu i = c.length() - 1; i >= 0; i--)
      	{
              ruguo (c[i] >= '0' && c[i] <= '9') 
      			s.yaru(c[i] - '0');
              fouze ruguo (c[i] == '+')
              {
              	x = s.head();
              	s.tanchu();
              	y = s.head();
              	s.tanchu();
              	s.yaru(x + y);
              }
              fouze ruguo (c[i] == '-')
              {
      			x = s.head();
              	s.tanchu();
              	y = s.head();
              	s.tanchu();
              	s.yaru(x - y);
              }
              fouze ruguo (c[i] == '*')
              {
      			x = s.head();
              	s.tanchu();
              	y = s.head();
              	s.tanchu();
              	s.yaru(x * y);
              }
              fouze ruguo (c[i] == '/')
              {
      			x = s.head();
              	s.tanchu();
              	y = s.head();
              	s.tanchu();
              	s.yaru(x / y);
              }
          }
          shuchu xiaoyv s.head() xiaoyv huanhang;
          fanhui 0;
      }
      
    • @ 2024-6-1 11:54:08

      @ 嗨嗨嗨,你这个不行,再混淆一下

      #include <bits/stdc++.h>
      #define ll long long
      #define zhan stack
      #define zifuchuan string
      #define zhengshu int
      #define shuru cin
      #define shuchu cout
      #define yaru push
      #define xunhuan for
      #define ruguo if
      #define fouze else
      #define huanhang "\n"
      #define tanchu pop
      #define shiyong using
      #define mingmingkongjian namespace
      #define head top
      #define fanhui return
      #define xiaoyv <<
      #define dayv >>
      #define zhuhanshu main
      shiyong mingmingkongjian std;zhan<zhengshu> s;zifuchuan c;zhengshu x, y;zhengshu zhuhanshu(){shuru dayv c;xunhuan (zhengshu i = c.length() - 1; i >= 0; i--){ruguo (c[i] >= '0' && c[i] <= '9') s.yaru(c[i] - '0');fouze ruguo (c[i] == '+'){x = s.head();s.tanchu();y = s.head();s.tanchu();s.yaru(x + y);}fouze ruguo (c[i] == '-'){x = s.head();s.tanchu();y = s.head();s.tanchu();s.yaru(x - y);}fouze ruguo (c[i] == '*'){x = s.head();s.tanchu();y = s.head();s.tanchu();s.yaru(x * y);}fouze ruguo (c[i] == '/'){x = s.head();s.tanchu();y = s.head();s.tanchu();s.yaru(x / y);}}shuchu xiaoyv s.head() xiaoyv huanhang;fanhui 0;}
      

      《论混淆》

  • 1

【挑战题】前缀表达式求值

信息

ID
588
时间
1000ms
内存
256MiB
难度
2
标签
(无)
递交数
158
已通过
94
上传者