Using the De Morgan Demogan law to understand the code

Source: Internet
Author: User

Demogan Law

Non (p and q) = (not P) or (not Q)
Non (p or q) = (not P) and (non-Q)

! (x && y) →!x | | !y! (x | | y) →!x &&!y


For example, find the three-digit minimum algorithm

if (x < y && x < z) min = x;else if (y < z) min = y;else min = z;

We are now using the law of De Morgan to see
The first if, the x < y && x < Z, if True then the minimum value is x, which is very well understood, the key is the following execution to else if (Y < Z) What is the condition? Nature is! (x < y && x < z) =x>=y | | x >=z, so if you run to this step, it means that x must be larger than the number of the two y,z, so it must not be the minimum, exclude, and then just determine the size of Y and Z is OK.

Using the De Morgan Demogan law to understand the code

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.