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

如题所述

答案是:等价
证明如下:
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 <stdio.h>

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.
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-06-18
等价

a b !(a||b) !a&&!b
0 0 1 1
0 1 0 0
1 0 0 0
1 1 0 0
相似回答