栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

2 0 2 0 年 第 十 一 届 蓝 桥 杯 - 省赛 - Python大学组 - B.寻找2020

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2 0 2 0 年 第 十 一 届 蓝 桥 杯 - 省赛 - Python大学组 - B.寻找2020

Ideas

这题其实没什么难度,主要就是要读取文件,整理成一个矩阵,然后遍历整个矩阵,每个是2的位置判断三个方向是否构成2020就可以了。

Code Python
if __name__ == '__main__':
    matrix = []
    with open("./2020.txt", 'r') as fp:
        for line in fp.readlines():
            line = line.strip()
            matrix.append(list(line))

    ans = 0
    for i in range(len(matrix)):
        for j in range(len(matrix[i])):
            if matrix[i][j] == '2':
                if j + 3 < len(matrix[i]) 
                        and matrix[i][j + 1] == '0' 
                        and matrix[i][j + 2] == '2' 
                        and matrix[i][j + 3] == '0':
                    ans += 1
                if i + 3 < len(matrix) 
                        and matrix[i + 1][j] == '0' 
                        and matrix[i + 2][j] == '2' 
                        and matrix[i + 3][j] == '0':
                    ans += 1
                if i + 3 < len(matrix) 
                        and j + 3 < len(matrix[i]) 
                        and matrix[i + 1][j + 1] == '0' 
                        and matrix[i + 2][j + 2] == '2' 
                        and matrix[i + 3][j + 3] == '0':
                    ans += 1
    print(ans)
Answer:16520
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/688715.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号