C Language if Question! if (d1 == d2 == d3)

Asked 2 weeks ago, Updated 2 weeks ago, 2 views

#include <stdio.h>

int main()
{
    int d1, d2, d3;   // dice1, dice2, dice3
    scanf("%d %d %d", &d1, &d2, &d3);

    If (d1 == d2 &&d1 == d3 &&d2 == d3) // The three dice are all equal
    { 
        printf("%d", 10000 + d1 * 1000);     
    }
    else if ((d1==d2&&d2!=d3) || (d2==d3&&d3!=d1) || (d1==d3&&d2!=d3) // if only two dice are equal
    {
        if (d1 == d2) // if d1 and d2 are equal
        {
            printf("%d", 1000 + d1 * 100);
        }
        else if (d1 == d3) // if d1 and d3 are equal
        {
            printf("%d", 1000 + d2 * 100);
        }
        else // if d2 and d3 are equal
        {
            printf("%d", 1000 + d3 * 100);
        }
    }
    else // if all three dice are different
    {
        if ((d1 > d2 &&d2 > d3) || (d1 > d3 &d3 > d2) // d1 is the largest
        {
            printf("%d", d1 * 100);
        }
        else if ((d2 > d1 &&d1 > d3) || (d2 > d3 & d3 > d1) //d2 is the largest
        {
            printf("%d", d2 * 100);
        }
        If else // d3 is the largest
        {
            printf("%d", d3 * 100);
        }
    }
    return 0;
}

I have a real question here

if (d1 == d2 == d3) <-- It doesn't work if the three dice are all the same.

Similarly

else if (d1==d2!=d3 || d1!=d2==d3 || d1==d3!=d2) <-- It doesn't work even if the conditional expression is the same when only two dice are the same?

So it works properly when I change it like the code above. Is there anything I don't understand about the conditional expression in the if statement? Is there a problem with the code above, too? Please give us feedback!

c

2022-09-20 10:50

1 Answers

When d1 d2 d3 is 2, 2, 2, and 2, respectively

d1 == d2 &&d2 == d3 &&d1 == d3 is

1 &&1 &&1 with a result of 1

Since d1 == d2 == d3 is calculated as (d1 == d2) == d3,

1 == d3 -> 1 == 2 -> 0.

Therefore, it's right to write && like above.

The question below is the same as the reason above


2022-09-20 10:50

If you have any answers or tips


© 2022 pinfo. All rights reserved.