请问C语言中 !(a||b) 和!a&&!b 等价吗?

2024-11-27 17:53:32
推荐回答(2个)
回答1:

答案是:等价
证明如下:
1. a = 0, b = 0时, a || b == 0, !(a || b) == 1, !a && !b == 1
2. a = 0, b = 1时, a || b == 1, !(a || b) == 0, !a && !b == 0
3. a = 1, b = 0时, 和2相同
4. a = 1, b = 1时, a || b == 1, !(a || b) == 0, !a && !b == 0

用代码证明如下:
#include

int main()
{
int a, b;

a = 0, b = 0;
if ((!(a || b)) == (!a && !b)) printf("Equal.\n");

a = 1, b = 0;
if ((!(a || b)) == (!a && !b)) printf("Equal.\n");

a = 0, b = 1;
if ((!(a || b)) == (!a && !b)) printf("Equal.\n");

a = 1, b = 1;
if ((!(a || b)) == (!a && !b)) printf("Equal.\n");

return 0;
}

输出结果如下:
Equal.
Equal.
Equal.
Equal.

回答2:

等价

a b !(a||b) !a&&!b
0 0 1 1
0 1 0 0
1 0 0 0
1 1 0 0