Be careful! # Define max (a, B) a & gt; B? A: B

Source: Internet
Author: User

When I was doing oj today, I defined two macros:

# Define max_2 (a, B) a> B? A: B max_3 (a, B, c) (a> B? A: B)> c? (A> B? A: B): c

Then the program results are always WA, and the logic of the program is carefully checked over and over again, so I am crazy. Finally, I found that there are problems with the two macros I have defined. Changed to the following:

Max_2 (a, B) (a> B? A: B) max_3 (a, B, c) (a> B? A: B)> c? (A> B? A: B): c)

 

The analysis process is as follows:

First, the above two problematic codes:

# Include <iostream> max_2 (x, y) x> y? X: y a = max_2 (,) + B = max_2 (,) + c = max_2 (, d = max_2 (, e = (max_2 (,) + <a <B <c <d <e <

Running result:

From the program variables c and d, we can find that max_2 (x, y) returns the correct results regardless of the order of x and y;

From the program variables a and B, we can find that the operation sequence is related to the order of x and y in max_2 (x, y). When x> y, the program first converts max_2 (2, 1) instead of adding 3 to B;

From the program variable e, we can find that even with parentheses, this operation sequence cannot be changed.

 

Another piece of problematic Code, for example, compilation fails:

 

Therefore, do not forget the key brackets. Otherwise, they will be harmless.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.