【题解】HDU.3350.#define is unsafe

描述

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.

輸入
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.

輸出
For each case, output two integers in a line separated by a single space.Integers in output won’t exceed 1000000.

實現

#include <stdio.h>
char s[1003], *p;
struct _ { int a, b; };/*a:表達式的值,b:表達式執行加法的次數*/
int getNum(char ** p) {
    /*返回*p指向的字符串開頭的數字數值,并將*p指向數字的后一位*/
    int t = 0;
    for(;**p>='0'&&**p<='9';t=t*10+*(*p)++-'0');
    return t;
}
_ f(){
    _ r = { 0 },t;
    int a[2] = { 0 }, b[2] = { 0 }, i = 0, n;
    /*  a[0],b[0]:表達式中逗號左邊的表達式的值和執行加法的次數
        a[1],b[1]:表達式中逗號右邊的表達式的值和執行加法的次數
    */
    for (;*p!=')' && *p!='\0'; p++) {
        switch (*p) {
        case 'M':
            /*執行到一個新的MAX()*/
            p += 4;
            t = f();
            b[i] += t.b;
            a[i] += t.a;
            break;
        case ',':i++;break;
        case '+':b[i]++;break;
        default:a[i] += getNum(&p),p--;break;/*執行到數字*/
        }
    }
    r.a = *a>a[1] ? *a : a[1];/*當前表達式的值爲逗號左右表達式中較大的*/
    r.b += *b + b[1] + (*a>a[1] ? *b : b[1]);/*當前表達式的加法次數爲 逗號左右表達式中結果較大的表達式的加法次數 與 這兩個表達式總共的加法次數 的和*/
    return r;
}
int main(void) {
    int T;
    for (scanf("%d", &T); T--;) {
        scanf("%s", p=s);
        _ r = f();
        printf("%d %d\n", r.a, r.b/2);/*去掉一半重複的加法*/
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/c_duoduo/article/details/51281672