目录

二灯游戏

三灯游戏


卡诺图的相关知识可以参考此处:https://blog.csdn.net/hahasusu/article/details/88244155

二灯游戏

一个游戏机有两个灯,一黄一绿,它们忽闪忽灭,你必须在出现以下情况的时候迅速按下游戏机:

  1. 绿灯灭,黄灯亮
  2. 绿灯、黄灯都灭
  3. 绿灯、黄灯都亮

解题思路:

1、首先定义两个基本的命题

  • 命题A:绿灯亮
  • 命题B:黄灯亮

2、画出卡诺图

卡诺图在程序中的应用

A为绿灯,0和1分别表示该灯的状态;

B为黄灯,同理。

根据游戏规则的3种情况,分别对应卡诺图种的:00、01、11

根据卡诺图每格具备相邻性的特点,使用1*1、1*2、2*2、4*4的网格圈出(仅能使用2^n个格子去圈)

卡诺图在程序中的应用

3、卡诺图化简

根据上图得出式子:(A`B` + A`B) + (A`B+AB)= A` + B  (A`表示A反)

由此得出只要满足 A` V(V表示或) B ,即绿灯灭 或者 黄灯亮即可按下按钮

4、验证

我们先不用卡诺图化简法来解决上述问题,以下是python代码:

'''
二灯游戏:
1、绿灯灭,黄灯亮
2、绿灯、黄灯都灭
3、绿灯、黄灯都亮
'''
def isPress(green, yellow):
    if green == False and yellow == True:
        return True
    elif green == False and yellow == False:
        return True
    elif green == True and yellow == True:
        return True
    return False


#绿灯灭,黄灯亮
status = isPress(green=False, yellow=True)
print(status)
#绿灯、黄灯都灭
status = isPress(green=False, yellow=False)
print(status)
#绿灯、黄灯都亮
status = isPress(green=True, yellow=True)
print(status)
#绿灯亮,黄灯灭
status = isPress(green=True, yellow=False)
print(status)

输出结果:

卡诺图在程序中的应用

卡诺图化简后的式子A` V B,代码:

def isPress2(green, yellow):
    if (not green) or yellow :
        return True
    return False

status = isPress2(green=False, yellow=True)
print(status)
status = isPress2(green=False, yellow=False)
print(status)
status = isPress2(green=True, yellow=True)
print(status)
status = isPress2(green=True, yellow=False)
print(status)

 

输出结果:

卡诺图在程序中的应用

由此可见使用卡诺图即可把复杂的句子简化成两行。

三灯游戏

将上述游戏进行升级,存在3种灯(绿灯、黄灯、红灯)的情况下,符合如下规则即可按下按钮:

  1. 3灯都灭
  2. 黄灯灭、红灯亮
  3. 绿灯灭、黄灯亮
  4. 3灯都亮

解题思路:

1、定义命题

  • 命题A:绿灯亮
  • 命题B:黄灯亮
  • 命题C:红灯亮

2、画出卡诺图,并将上述情况在卡诺图中用1来代替原先的二进制

卡诺图在程序中的应用

上图将4个规则在卡诺图中对应出来。

3、卡诺图化简

根据卡诺图每格具备相邻性的特点,进行如下的画圈:

卡诺图在程序中的应用

因此可得式子:(A`B`C`+A`B`C+A`BC+A`BC`) + (A`B`C+A`BC+AB`C+ABC)

= (A`B` + A`B) + (A`C +AC)

= A` + C

化简后获得 A` + C

4、验证

正常解法:

def isThreeLightPress(green, yellow, red):
    if green == False and yellow == False and red == False:
        return True
    elif yellow == False and red:
        return True
    elif green == False and yellow:
        return True
    elif green and yellow and red:
        return True
    return False
# 全灯灭
status = isThreeLightPress(green=False, yellow=False, red=False)
print(status)
# 黄灯灭、红灯亮(不在乎绿灯的状态,因此都加上)
status = isThreeLightPress(green=False, yellow=False, red=True)
print(status)
status = isThreeLightPress(green=True, yellow=False, red=True)
print(status)
# 绿灯灭、黄灯亮(不在乎红灯的状态,因此都加上)
status = isThreeLightPress(green=False, yellow=True, red=False)
print(status)
status = isThreeLightPress(green=False, yellow=True, red=True)
print(status)
# 全灯亮
status = isThreeLightPress(green=True, yellow=True, red=True)
print(status)

输出结果:

卡诺图在程序中的应用

使用卡诺图化简法:

#卡诺图改造为not green or red
def isThreeLightPress2(green, yellow, red):
    if not green or red:
        return True
    return False
print('---------this is a split----------')
# 全灯灭
status = isThreeLightPress2(green=False, yellow=False, red=False)
print(status)
# 不在乎绿灯的状态,因此都加上
status = isThreeLightPress2(green=False, yellow=False, red=True)
print(status)
status = isThreeLightPress2(green=True, yellow=False, red=True)
print(status)
# 不在乎红灯的状态,因此都加上
status = isThreeLightPress2(green=False, yellow=True, red=False)
print(status)
status = isThreeLightPress2(green=False, yellow=True, red=True)
print(status)
# 全灯亮
status = isThreeLightPress2(green=True, yellow=True, red=True)
print(status)

输出结果:

卡诺图在程序中的应用

相关文章: