Python 蓝桥杯 七段码

一、题目描述

 二、题目分析

我使用的是暴力解决,因为一共就7位,但是容易出现漏算或多算,经过很久的找错误和改正才得到答案80.一个较易弄错的问题就是出现3中组合:abde、afcd、bcef,如果使用暴力判断各个字符周围是否有字符,会得到答案83,就是因为没有考虑的以上3中情况,4个二极管两两连续,但是整体不连续,是不符合题意的,需要排除。

这里使用的方法不推荐,因为是在已知答案为80的情况下,如果不知道,很容易得到错误答案83.

还有一位up主,也用到了排列组合,原文链接如下:https://blog.csdn.net/file543102118/article/details/115278220

主要步骤是:将图存入字典中,对字典的key使用字符串的join方法聚合在一起。对字符串的前i个字符进行无序组合,接着对各个无序组合进行判断,和暴力求解一样,如果通过遍历进行判断,组合中至少要有2个元素,当只有一个元素时,一定是满足题意的。当有2个以上元素时,还需要进行有序排列,这样可以避免我自己思考问题中出现的3中情况。这里用到了for ...else循环

在 Python 中,for ...else语句 表示这样的意思:for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while ...else语句也是一样。

换句话说,当for所有的循环代码块正常运行完,才会运行else语句。

三、源代码

import itertools
num = 0

def check(a):
    def define(a):
        for i in range(7):
            if a[i] != 0:
                a[i] = 1
        if a.count(1)==1:
            return True
            
        if a[0]==1:# a亮
            if a[1]==0 and a[5]==0:
                return False
        if a[1]==1:# b亮
            if a[0]==0 and a[2]==0 and a[6]==0:
                return False
        if a[2]==1:
            if a[1]==0 and a[3]==0 and a[6]==0:
                return False
        if a[3]==1:
            if a[2]==0 and a[4]==0:
                return False
        if a[4]==1:
            if a[3]==0 and a[5]==0 and a[6]==0:
                return False
        if a[5]==1:
            if a[0]==0 and a[6]==0 and a[4]==0:
                return False
        if a[6]==1:
            if a[1]==0 and a[2]==0 and a[4]==0 and a[5]==0:
                return False
        if a==[1,1,0,1,1,0,0] or a==[1,0,1,1,0,1,0] or a==[0,1,1,0,1,1,0]:
            return False
        return True
    if not define(a):
        return 0
    else:
        return 1
        
        
for a in range(1,8):
    for item in itertools.combinations('abcdefg',a):
##        print(item)
        ans = [0 for i in range(7)]
        for i in item:
##            print("item",item)
##            print(ord(i)-97)
            ans[ord(i)-97] = 1
##        print("ans:",ans)
        num += check(ans)
##        print("num:",num)
print(num)

较好的方法的代码:

import itertools
new_list = []
# 计数
num = 0
# 采用字典将该标号相接的所有标号列举出来
dict = {'a':['f', 'b'], 'b':['a', 'c', 'g'], 'c':['b', 'd', 'g'],
        'd':['e', 'c'], 'e':['d', 'f', 'g'], 'f':['a', 'e', 'g'],
        'g':['b', 'c', 'e', 'f']}

string = "".join(list(dict.keys()))
for i in range(1, 8):
    # 对string字符串中的前i个字符进行无序组合
    for j in (itertools.combinations(list(string), i)):
        new_list.append("".join(j))
##print("new_list:",new_list)
for str1 in new_list:
    if len(str1) == 1:
        num += 1
        continue

    for situation in itertools.permutations(str1):
##        print("str1:",str1)
##        print("situation",situation)
        for c in range(1, len(situation)):
            if situation[c - 1] not in dict[situation[c]]:
                break

        else:
            num += 1
            break
print(num)
# num = 80

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值