2020年第十一届蓝桥杯 - 省赛 - Python大学组 - B.寻找2020
生活随笔
收集整理的這篇文章主要介紹了
2020年第十一届蓝桥杯 - 省赛 - 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 = 0for 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 += 1if i + 3 < len(matrix) \and matrix[i + 1][j] == '0' \and matrix[i + 2][j] == '2' \and matrix[i + 3][j] == '0':ans += 1if 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 += 1print(ans)Answer:16520
總結
以上是生活随笔為你收集整理的2020年第十一届蓝桥杯 - 省赛 - Python大学组 - B.寻找2020的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2 0 2 0 年 第 十 一 届 蓝
- 下一篇: LeetCode Algorithm 剑