Advertisement

第十一届蓝桥杯大赛第二场省赛试题------蓝桥杯Python大学组

阅读量:

目录

A

B

C


欢迎互相交流学习

A

复制代码
 count = 0

    
 for i in range(1, 2021):
    
     j = str(i)
    
     if '2' in j:
    
     count += 1
    
 print(count)
    
 # 563

B

复制代码
 # import random

    
 #
    
 # # 定义矩阵的行数和列数
    
 # rows = 500
    
 # cols = 400
    
 #
    
 # a = 0
    
 # # 随机生成2和0的矩阵
    
 # matrix = [[random.choice([2, 0]) for _ in range(10)] for _ in range(8)]
    
 #
    
 # # 将矩阵写入文件
    
 # with open('2020.txt', 'w') as file:
    
 #     for row in matrix:
    
 #         file.write(' '.join(map(str, row)) + '\n')
    
 #
    
 # print("矩阵已写入到2020.txt文件中。")
    
 #
    
 # 打开文件并读取内容
    
 with open('2020.txt', 'r') as file:
    
     # 逐行读取文件内容并将每行拆分成整数,形成一个二维列表
    
     matrix = [list(map(int, line.strip().split())) for line in file]
    
 row = len(matrix)
    
 col = len(matrix[0])
    
 # 现在,matrix1 包含了从文件中读取的内容
    
 print(matrix)
    
 print(row)
    
 print(col)
    
 count = 0
    
 for i in range(0, row - 3):
    
     for j in range(0, col - 3):
    
     if matrix[i][j] == 2 and matrix[i+1][j+1] == 0 and matrix[i+2][j+2] == 2 and matrix[i+3][j+3] == 0:
    
         count += 1
    
     if matrix[i][j] == 2 and matrix[i][j+1] == 0 and matrix[i][j+2] == 2 and matrix[i][j+3] == 0:
    
         count += 1
    
     if matrix[i][j] == 2 and matrix[i+1][j] == 0 and matrix[i+2][j] == 2 and matrix[i+3][j] == 0:
    
         count += 1
    
 print(count)

C

复制代码
 # from 2000年1月1日周六 to 2020年10月1日周四

    
 # 1 3 5 7 8 10 12 31天
    
 # 4 6 9 11 30天
    
 # 2 闰年 29 平年 28
    
 year = 2000
    
 month = 1
    
 day = 1
    
 week = 6
    
 count = 0
    
 while 1:
    
     count += 1
    
     if day == 1 or week == 1:
    
     count += 1
    
     #      到达日期跳出循环
    
     if year == 2020 and month == 10 and day == 1:
    
     break
    
     #     week增加部分
    
     if week == 7:
    
     week = 1
    
     else:
    
     week += 1
    
     #     年月份增加部分
    
     if month in [1, 3, 5, 7, 8, 10, 12]:
    
     if day == 31:
    
         day = 1
    
         if month == 12:
    
             month = 1
    
             year += 1
    
         else:
    
             month += 1
    
     else:
    
         day += 1
    
     if month in [4, 6, 9, 11]:
    
     if day == 30:
    
         day = 1
    
         month += 1
    
     else:
    
         day += 1
    
     if month == 2:
    
     if year % 4 == 0:
    
         if day == 29:
    
             day = 1
    
             month = 3
    
         else:
    
             day += 1
    
     else:
    
         if day == 28:
    
             day = 1
    
             month = 3
    
         else:
    
             day += 1
    
 print(count)
    
 # 8667

全部评论 (0)

还没有任何评论哟~