Problem B
Miscalculation
Input: Standard Input
Time Limit: 1 second
Bob is an elementary schoolboy, not so good at mathematics. He found Father’s calculator and tried cheating on his homework using it. His homework was calculating given expressions containing multiplications and additions. Multiplications should be done prior to additions, of course, but the calculator evaluates the expression from left to right, neglecting the operator precedence. So his answers may be the result of either of the following two calculation rules.
? Doing multiplication before addition ? Doing calculation from left to right neglecting the operator precedence
Write a program that tells which of the rules is applied from an expression and his answer.
An expression consists of integers and operators. All the integers have only one digit, from 0 to 9. There are two kinds of operators + and *, which represent addition and multiplication, respectively.
The following is an example expression.
1+2*3+4
Calculating this expression with the multiplication-?rst rule, the answer is 11, as in Sample Input 1. With the left-to-right rule, however, the answer will be 13 as shown in Sample Input 2.
There may be cases in which both rules lead to the same result and you cannot tell which of the rules is applied. Moreover, Bob sometimes commits miscalculations. When neither rules would result in Bob’s answer, it is clear that he actually did.
Input
The input consists of a single test case speci?ed with two lines. The ?rst line contains the expression to be calculated. The number of characters of the expression is always odd and less than or equal to 17. Each of the odd-numbered characters in the expression is a digit from ‘0’ to ‘9’. Each of the even-numbered characters is an operator ‘+’ or ‘*’. The second line contains an integer which ranges from 0 to 999999999, inclusive. This integer represents Bob’s answer for the expression given in the ?rst line.
Output
Output one of the following four characters:
M When only the multiplication-?rst rule results Bob’s answer.
L When only the left-to-right rule results Bob’s answer.
U When both of the rules result Bob’s answer.
I When neither of the rules results Bob’s answer.
Sample Input 1 Sample Output 1
1+2*3+4 11
M
Sample Input 2 Sample Output 2
1+2*3+4 13
L
Sample Input 3 Sample Output 3
3 3
U
Sample Input 4 Sample Output 4
1+2*3+4 9
I
这道题有俩种解法,第一个可以用数组,第二种可以用栈,其实思路都差不多的
第一种 数组
1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 char a[100]; 6 int b[100],c[100]; 7 int n,m,x,y=0,i; 8 scanf("%s",a); 9 scanf("%d",&n); 10 for(i=0;i<strlen(a);i+=2) 11 { 12 b[i]=a[i]-‘0‘; 13 c[i]=a[i]-‘0‘; 14 //printf("%d\n",b[i]); 15 } 16 for(i=1;i<strlen(a);i+=2) 17 { 18 if(a[i]==‘+‘) 19 { 20 b[i+1]=b[i-1]+b[i+1]; 21 } 22 if(a[i]==‘*‘) 23 { 24 b[i+1]=b[i-1]*b[i+1]; 25 } 26 //printf("%d\n",b[i+1]); 27 28 } 29 //printf("%d\n",i); 30 x=b[i-1]; 31 //printf("%d\n",x); 32 for(i=1;i<strlen(a);i+=2) 33 { 34 if(a[i]==‘*‘) 35 { 36 c[i+1]=c[i-1]*c[i+1]; 37 //printf("%d\n",c[i+1]); 38 c[i-1]=0; 39 } 40 } 41 for(i=0;i<strlen(a);i+=2) 42 { 43 y+=c[i]; 44 //printf("%d\n",y); 45 } 46 if(n==x&&n==y) 47 { 48 printf("U\n"); 49 return 0; 50 } 51 if(n==x) 52 { 53 printf("L\n"); 54 return 0; 55 } 56 if(n==y) 57 { 58 printf("M\n"); 59 return 0; 60 } 61 if(n!=x&&n!=y) 62 { 63 printf("I\n"); 64 return 0; 65 } 66 }
第二种 栈
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<iostream> 6 #include<algorithm> 7 #include<queue> 8 #include<map> 9 #include<stack> 10 11 using namespace std; 12 13 int main() { 14 char a[20]; 15 while (~scanf("%s",a)){ 16 getchar(); 17 long long n; 18 scanf("%lld",&n); 19 getchar(); 20 21 if(strlen(a)==1){ 22 if(a[0]==n+48) 23 cout<<"U"<<endl; 24 else 25 cout<<"I"<<endl; 26 continue; 27 } 28 29 long long l = 0; 30 31 l+=a[0]-48; 32 for(int i = 1;i<strlen(a);i++){ 33 if(a[i] == ‘+‘){ 34 i++; 35 l+= a[i]-48; 36 } 37 else if(a[i] == ‘*‘){ 38 i++; 39 l *= a[i]-48; 40 } 41 } 42 43 44 long long m = 0; 45 stack<long long>s; 46 stack<char>f; 47 f.push(‘+‘); 48 for(int i = 0;i<strlen(a);i++){ 49 if(a[i]==‘+‘ || a[i]==‘*‘){ 50 f.push(a[i]); 51 } 52 else{ 53 s.push((long long)a[i]-48); 54 } 55 } 56 57 while (!f.empty()){ 58 if(f.top() == ‘+‘){ 59 m+=s.top(); 60 s.pop(); 61 f.pop(); 62 } 63 else{ 64 long long x1 = s.top(); 65 s.pop(); 66 long long x2 = s.top(); 67 s.pop(); 68 s.push((x1)*(x2)); 69 f.pop(); 70 } 71 } 72 73 74 if(m==n && l==n){ 75 cout<<"U"<<endl; 76 } 77 else if(m==n){ 78 cout<<"M"<<endl; 79 } 80 else if(l==n){ 81 cout<<"L"<<endl; 82 } 83 else{ 84 cout<<"I"<<endl; 85 } 86 } 87 return 0; 88 }