模拟/字符串处理 UVALive 6833 Miscalculatio

题目传送门

  1 /*
  2     模拟/字符串处理:主要是对*的处理,先把乘的预处理后再用加法,比如说是:1+2*3+4 = 1+..6+4 = 11
  3 */
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstring>
  7 #include <string>
  8 #include <cmath>
  9 #include <vector>
 10 #include <map>
 11 #include <queue>
 12 using namespace std;
 13 
 14 typedef long long ll;
 15 const int MAXN = 1e2 + 10;
 16 const int INF = 0x3f3f3f3f;
 17 char s[MAXN];
 18 int len;
 19 
 20 void solve(void)
 21 {
 22     int p1 = 0, p2 = 0;
 23     for (int i=1; i<=len; ++i)
 24     {
 25         if (s[i] == '*')
 26         {
 27             p1 = p2 = i;
 28             while (p1 >= 1 && s[p1] != '+')    p1--;    p1++;
 29             while (p2 <= len && s[p2] != '+')    p2++;    p2--;
 30             ll tmp = 0;
 31             for (int j=p1; j<=p2; j+=2)
 32             {
 33                 if (j == p1)    tmp = s[j] - '0';
 34                 else    tmp = tmp * (s[j] - '0');
 35             }
 36 //            printf ("p1: %d p2: %d
tmp: %d
", p1, p2, tmp);
 37             int p = p2;
 38             while (tmp)
 39             {
 40                 s[p--] = tmp % 10 + '0';
 41                 tmp /= 10;
 42             }
 43             for (int j=p1; j<=p; ++j)    s[j] = '.';
 44         }
 45     }
 46 }
 47 
 48 int main(void)        //UVALive 6833 Miscalculation
 49 {
 50 //    freopen ("B.in", "r", stdin);
 51 
 52     while (scanf ("%s", s + 1) == 1)
 53     {
 54         ll y;    scanf ("%lld", &y);
 55         ll sum_l = 0, sum_m = 0;
 56 
 57         len = strlen (s + 1);    char op;    int x = 0;
 58         for (int i=1; i<=len; ++i)
 59         {
 60             if (i & 1)
 61             {
 62                 if (i == 1)    sum_l = s[i] - '0';
 63                 else
 64                 {
 65                     if (op == '+')    sum_l += s[i] - '0';
 66                     else    sum_l = sum_l * (s[i] - '0');
 67                 }
 68             }
 69             else    op = s[i];
 70         }
 71 
 72         solve ();
 73         for (int i=1; i<=len; ++i)
 74         {
 75             if (s[i] == '.' || s[i] == '+')    continue;
 76             else if (s[i] >= '0' && s[i] <= '9')
 77             {
 78                 ll res = 0;    int j;
 79                 for (j=i; j<=len; ++j)
 80                 {
 81                     if (s[j] == '+')    break;
 82                     res = res * 10 + (s[j] - '0');
 83                 }
 84                 sum_m += res;
 85                 i = j - 1;
 86             }
 87         }
 88 
 89         if (y == sum_l && y == sum_m)    puts ("U");
 90         else if (y != sum_l && y != sum_m)    puts ("I");
 91         else
 92         {
 93             if (y == sum_l)    puts ("L");
 94             else    puts ("M");
 95         }
 96 
 97 //        printf ("%s
", s + 1);
 98 //        printf ("%lld %lld 
", sum_l, sum_m);
 99     }
100 
101     return 0;
102 }
编译人生,运行世界!
原文地址:https://www.cnblogs.com/Running-Time/p/4558848.html