题意:给你一个算式,问你乘法优先 和 从左到右计算的答案。

解题思路:水+乱搞

解题代码:

  1 // File Name: b.cpp
  2 // Author: darkdream
  3 // Created Time: 2015年03月25日 星期三 12时32分43秒
  4 
  5 #include<vector>
  6 #include<list>
  7 #include<map>
  8 #include<set>
  9 #include<deque>
 10 #include<stack>
 11 #include<bitset>
 12 #include<algorithm>
 13 #include<functional>
 14 #include<numeric>
 15 #include<utility>
 16 #include<sstream>
 17 #include<iostream>
 18 #include<iomanip>
 19 #include<cstdio>
 20 #include<cmath>
 21 #include<cstdlib>
 22 #include<cstring>
 23 #include<ctime>
 24 #define LL long long
 25 
 26 using namespace std;
 27 char str[1000];
 28 int tt;
 29 int a[100];
 30 int op[100];
 31 int m ;
 32 int solve1()
 33 {
 34      int ta[100];
 35      int tp[100];
 36      memcpy(ta,a,sizeof(a));
 37      memcpy(tp,op,sizeof(op));
 38      int tk = tt; 
 39      int ok = 0; 
 40      while(!ok)
 41      {
 42          ok = 1; 
 43          for(int i = 1;i < tk ;i ++)
 44          {
 45            if(tp[i] == 2 )
 46            {
 47               ok = 0 ; 
 48               ta[i] = ta[i]*ta[i+1];
 49               for(int j = i+2;j <= tk ;j ++)
 50               {
 51                   ta[j-1] = ta[j];
 52                   tp[j-2] = tp[j-1];
 53               }
 54               tk -- ; 
 55               break;
 56            }
 57          }
 58      }
 59      int ans = ta[1];
 60      for(int i = 1;i < tk;i ++)
 61      {
 62         if(tp[i] == 1) 
 63             ans += ta[i+1];
 64         else ans *= ta[i+1];
 65      }
 66      //printf("%d\n",ans);
 67      if(ans == m )
 68          return 1; 
 69      return 0 ;
 70      
 71 }
 72 int solve2()
 73 {
 74      int ans = a[1];
 75      for(int i = 1;i < tt;i ++)
 76      {
 77         if(op[i] == 1) 
 78             ans += a[i+1];
 79         else ans *= a[i+1];
 80      }
 81      //printf("%d\n",ans);
 82      if(ans == m )
 83          return 1; 
 84      return 0 ;
 85 }
 86 int main(){
 87     scanf("%s",str);
 88     scanf("%d",&m);
 89     int len = strlen(str); 
 90     int t = 0 ;
 91     tt = 0 ; 
 92     for(int i = 0 ;i < len ;i ++)
 93     {
 94         if(str[i] <= '9' && str[i] >= '0')    
 95         {
 96            t = t * 10 + str[i] - '0' ;   
 97         }else{
 98            tt ++ ;
 99            //printf("***\n");
100            if(str[i] == '+')
101              {
102                op[tt] = 1;
103              }else op[tt] = 2; 
104            a[tt] = t ;
105            t = 0 ;
106         }
107     }
108     tt ++ ; 
109     a[tt] = t;
110     int tt1 = solve1();
111     int tt2 = solve2();
112     if(tt1&& tt2)
113     {
114        puts("U");
115     }else if(tt1)
116     {
117        puts("M");
118     }else if(tt2)
119         puts("L");
120     else puts("I");
121 return 0;
122 }
View Code

 

posted on 2015-03-25 21:29  dark_dream  阅读(294)  评论(0编辑  收藏  举报