![]() |
||||||||||
|
||||||||||
#define is unsafeTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 741 Accepted Submission(s): 468 Problem Description Have you used #define in C/C++ code like the code below? #include <stdio.h> #define MAX(a , b) ((a) > (b) ? (a) : (b)) int main() { printf("%d\n" , MAX(2 + 3 , 4)); return 0; } Run the code and get an output: 5, right? You may think it is equal to this code: #include <stdio.h> int max(a , b) { return ((a) > (b) ? (a) : (b)); } int main() { printf("%d\n" , max(2 + 3 , 4)); return 0; } But they aren't.Though they do produce the same anwser , they work in two different ways. The first code, just replace the MAX(2 + 3 , 4) with ((2 + 3) > (4) ? (2 + 3) : 4), which calculates (2 + 3) twice. While the second calculates (2 + 3) first, and send the value (5 , 4) to function max(a , b) , which calculates (2 + 3) only once. What about MAX( MAX(1+2,2) , 3 ) ? Remember "replace". First replace: MAX( (1 + 2) > 2 ? (1 + 2) : 2 , 3) Second replace: ( ( ( 1 + 2 ) > 2 ? ( 1 + 2 ) : 2 ) > 3 ? ( ( 1 + 2 ) > 2 ? ( 1 + 2 ) : 2 ) : 3). The code may calculate the same expression many times like ( 1 + 2 ) above. So #define isn't good.In this problem,I'll give you some strings, tell me the result and how many additions(加法) are computed. Input The first line is an integer T(T<=40) indicating case number. The next T lines each has a string(no longer than 1000), with MAX(a,b), digits, '+' only(Yes, there're no other characters). In MAX(a,b), a and b may be a string with MAX(c,d), digits, '+'.See the sample and things will be clearer. Output For each case, output two integers in a line separated by a single space.Integers in output won't exceed 1000000. Sample Input
Sample Output
Author madfrog Source | ||||||||||
|