UVAlive 6833 Miscalculation 字符串处理

去年省选的题

因为卡了这道题再加上队友占机时 省选第一天华丽爆零了

用事实证明了1+1+1<1的事实

毕竟下半年单挑了东北赛名额 省赛打不出来名额就真的就不怪我了(摔

现在有拿出来做 长个记性 希望今年省选可以卷土重来

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 int way(char str[]) //
 5 {
 6     int ans=str[0]-'0';
 7     int now=1;
 8     while(str[now])
 9     {
10         if(str[now]=='*')
11         {
12             now++;
13             ans*=str[now]-'0';
14         }
15         else
16         {
17             now++;
18             ans+=str[now]-'0';
19         }
20         now++;
21     }
22     return ans;
23 }
24 
25 int way1(char str[])
26 {
27     int now=0;
28     stack<int>st;
29     st.push(str[now++]-'0');
30     while(str[now])
31     {
32         int num=str[now+1]-'0';
33         if(str[now]=='+')
34         {
35             st.push(num);
36         }
37         else
38         {
39             int tmp=st.top();
40             st.pop();
41             st.push(tmp*num);
42         }
43         now+=2;
44     }
45     int ans=0;
46     while(!st.empty())
47     {
48         ans+=st.top();
49         st.pop();
50     }
51     return ans;
52 }
53 
54 int main()
55 {
56     char str[20];
57     while(~scanf("%s",str))
58     {
59         int ans;
60         scanf("%d",&ans);
61         int x=way(str);
62         int y=way1(str);
63         if(x==ans&&y==ans) printf("U\n");
64         else if(x==ans) printf("L\n"); //
65         else if(y==ans) printf("M\n");
66         else printf("I\n");
67     }
68     return 0;
69 }
70 /*
71 
72 1+2*3+4
73 11
74 1+2*3+4
75 13
76 3
77 3
78 1+2*3+4
79 9
80 
81 */

 

posted @ 2017-01-15 20:21  良将ℓ  阅读(273)  评论(0编辑  收藏  举报