zoukankan      html  css  js  c++  java
  • csu

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1537

    因为给出的式子是必定合法的,只要用两个栈分别保存符号和数字.算出答案后和从左至右算的答案比对即可.

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <vector>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <string>
     8 #include <set>
     9 #include <functional>
    10 #include <numeric>
    11 #include <sstream>
    12 #include <stack>
    13 #include <map>
    14 #include <queue>
    15 
    16 #define CL(arr, val)    memset(arr, val, sizeof(arr))
    17 
    18 #define ll long long
    19 #define inf 0x7f7f7f7f
    20 #define lc l,m,rt<<1
    21 #define rc m + 1,r,rt<<1|1
    22 #define pi acos(-1.0)
    23 
    24 #define L(x)    (x) << 1
    25 #define R(x)    (x) << 1 | 1
    26 #define MID(l, r)   (l + r) >> 1
    27 #define Min(x, y)   (x) < (y) ? (x) : (y)
    28 #define Max(x, y)   (x) < (y) ? (y) : (x)
    29 #define E(x)        (1 << (x))
    30 #define iabs(x)     (x) < 0 ? -(x) : (x)
    31 #define OUT(x)  printf("%I64d
    ", x)
    32 #define lowbit(x)   (x)&(-x)
    33 #define Read()  freopen("din.txt", "r", stdin)
    34 #define Write() freopen("dout.txt", "w", stdout);
    35 
    36 
    37 using namespace std;
    38 
    39 int main()
    40 {
    41     //freopen("a.txt","r",stdin);
    42     char s[25];
    43     int num;
    44     scanf("%s",s);
    45     scanf("%d",&num);
    46     stack<int>s1;
    47     stack<char>s2;
    48     int l=strlen(s);
    49     for(int i=0;i<l;i++)
    50     {
    51         if(s[i]>='0'&&s[i]<='9') s1.push(s[i]-'0');
    52         else if(s[i]=='*')
    53         {
    54             int x=s1.top();s1.pop();
    55             int y=s[i+1]-'0';
    56             s1.push(x*y);
    57             i++;
    58         }
    59         else s2.push('+');
    60     }
    61     while(!s2.empty())
    62     {
    63         int x=s1.top();s1.pop();
    64         int y=s1.top();s1.pop();
    65         s1.push(x+y);
    66         s2.pop();
    67     }
    68     int sum=s[0]-'0';
    69     for(int i=1;i<l;i++)
    70     {
    71         if(s[i]=='+') sum=sum+(s[i+1]-'0');
    72         else if(s[i]=='*') sum=sum*(s[i+1]-'0');
    73     }
    74     //printf("%d %d
    ",s1.top(),sum);
    75     if(s1.top()==sum&&sum==num) printf("U
    ");
    76     else if(s1.top()!=num&&sum!=num) printf("I
    ");
    77     else if(s1.top()==num&&sum!=num) printf("M
    ");
    78     else if(s1.top()!=num&&sum==num) printf("L
    ");
    79     return 0;
    80 }
  • 相关阅读:
    layUI--实现分页
    maven项目-----Dynamic Web Module 3.0 requires Java 1.6 or newer
    js---open打开新窗口
    hibernate常见问题?
    eclipse如何修改dynamic web module version
    设计模式之五种创建型模式学习笔记
    设计模式学习笔记
    数据定义语言(DDL Data Definition Language)基础学习笔记
    idea安装成功后,设置字体、快捷键、配置jdk等操作
    基于Redis实现分布式锁
  • 原文地址:https://www.cnblogs.com/nowandforever/p/4605610.html
Copyright © 2011-2022 走看看