News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

2021 The 12th Blue Bridge Cup Provincial Competition Python University Group real questions plus analysis (updating)

posted on 2023-05-07 20:06     read(560)     comment(0)     like(6)     collect(2)


Table of contents

1. Question A: Cards

2. Question B: Straight line

3. Question C: Cargo placement

4. Question D: Path 

5. Loop count

8. Question H: Left Child Right Brother

9. Question I: XOR Sequence

10. Item J: Bracket Sequence

11. Space

12. Jump

13. The Wounded Queen (2021 Mock Questions)

14. Delete characters

15. Multiply

16. Irrigation (simulation competition questions)

17. Minesweeper

18. Bidirectional sorting

19. Negotiation

20. Minimum weight

21. Distance and

22. Time addition (simulation questions)

23. Arrange the balls

24. Number of sequences

25. Legal date

26. Blurred image

27. Number of digits

28. Time display



1. Question A: Cards

Idea: There are 2021 numbers for each number from 0 to 9, and 1 is definitely the fastest to use, which is to calculate a total of 2021 numbers between 1 and a certain number 1

code:

  1. s=0
  2. for i in range(1,5000):
  3. s=s+str(i).count('1')
  4. if s==2021:
  5. print(i)
  6. break

Final answer: 3181

2. Question B: Straight line

 Idea: (Boring ideas from others) At the beginning, I was still thinking about how to use the form of y=kx+b to represent a straight line. There is no way to find the slope of a vertical line in this form, and special circumstances must be considered. Later, I saw a big guy on the Internet saying that a two-point straight line equation can be used:
(y1-y2) * x +(x2-x1) * y +( x1 * y2 - x2 * y1)=0

The final calculated result is Ax+By+C=0in the format, as long as A, B, and C after the reduction are not the same, they can be regarded as different straight lines.

code:

  1. dian=[]
  2. for i in range(0,20):
  3. for j in range(0,21):
  4. dian.append((i,j))
  5. result=set()
  6. def gys(x,y):#求x,y的最大公约数
  7. if y==0:
  8. return x
  9. return gys(y,x%y)
  10. for i in range(len(dian)-1):
  11. x1,y1=dian[i]
  12. for j in range(i+1,len(dian)):
  13. x2,y2=dian[j]
  14. a=y1-y2
  15. b=x2-x1
  16. c=x1*y2-x2*y1
  17. k=gys(gys(a,b),c)
  18. result.add((a/k,b/k,c/k))
  19. print(len(result))

The set() collection itself has the characteristic of not repeating.

For the set() collection, adding elements to it should use the add function. Distinct from append.

3. Question C: Cargo placement

 Idea: L, W, and H are all factors of n, so first find all the factors of n and put them in the sequence. Then perform a three-layer loop on all the numbers in the sequence to see how many schemes meet the conditions.

code:

  1. import math
  2. n = 2021041820210418
  3. fac = []
  4. for i in range(1, int(math.sqrt(n)) + 1):
  5. if n % i == 0:
  6. fac.append(i)
  7. fac.append(int(n / i))
  8. x = 0
  9. for l in fac:
  10. for k in fac:
  11. for m in fac:
  12. if l * k * m == n:
  13. x = x + 1
  14. print(x)

4. Question D: Path 

Ideas: This question adopts the idea of ​​dynamic programming. dp[i] represents the shortest path length between node 1 and node i.

code:

  1. dp=[float('inf')]*2022
  2. def gbs(x,y):#求x和y的最小公倍数
  3. x1=x
  4. y1=y
  5. while y:
  6. x,y=y,x%y
  7. return (x1*y1//x)
  8. for i in range(1,22):
  9. dp[i]=i
  10. for i in range(22,2022):
  11. for j in range(1,22):
  12. dp[i]=min(dp[i],dp[i-j]+gbs(i,i-j))
  13. print(dp[2021])

Answer: 10266837

5. Loop count

Refer to this: (143 messages) The 2021 12th Blue Bridge Cup Software Provincial Competition python group test questions and their analysis. _Kinght_123's Blog-CSDN Blog_2021 Blue Bridge Cup python

6. Question F: Time display

 code:

  1. n=int(input())
  2. a=24*60*60*1000
  3. c=n%a
  4. hour=c//(60*60*1000)
  5. minu=(c%(60*60*1000))//(60*1000)
  6. sec=(c%(60*1000))//1000
  7. print('%02d'%hour,end=':')
  8. print('%02d'%minu,end=':')
  9. print('%02d'%sec,end='')

 7. Question G: Yang Hui Triangle

 

 Ideas for reference: (156 messages) Preparing for the Blue Bridge Cup Over the Years Exam Questions: Yang Hui Triangular Provincial Competition Group B Python Detailed Explanation_m0_62277756's Blog-CSDN Blog

code:

  1. n=int(input())
  2. def c(a,b):
  3. res=1
  4. i,j=a,1
  5. while j<=b:
  6. res=res*i/j
  7. i-=1
  8. j+=1
  9. return int(res)
  10. def find(j,n):
  11. l=2*j
  12. r=n
  13. while l<=r:
  14. mid=(l+r)//2
  15. if c(mid,j)==n:
  16. print(int(mid*(mid+1)/2)+j+1)
  17. return True
  18. elif c(mid,j)>n:
  19. r=mid-1
  20. else:
  21. l=mid+1
  22. return False
  23. for i in range(16,-1,-1):
  24. if find(i,n):
  25. break

8. Question H: Left Child Right Brother

 

 Code: (successful operation, the code is posted by someone else on the Lanqiao official website)

  1. import sys
  2. sys.setrecursionlimit(1000000)
  3. n=int(input())
  4. global ans
  5. ans=0
  6. def dfs(u):
  7. global ans
  8. tot=len(s[u])
  9. for i in range(tot):
  10. v=s[u][i]
  11. f[v]=f[u]+tot
  12. ans=max(ans,f[v])
  13. dfs(v)
  14. s=[[] for i in range(n+1)]
  15. f=[0]*(n+1)
  16. for i in range(n-1):s[int(input())].append(i+2)
  17. dfs(1)
  18. print(ans)

9. Question I: XOR Sequence

10. Item J: Bracket Sequence

11. Space

 This is quite simple: 1MB=1024KB 1KB=1024B 1B=8bit

code:

  1. a=256*1024*1024*8//32
  2. print(a)

12. Jump

topic description

Xiaolan plays a game in a grid with n rows and m columns.

At the beginning, Xiaolan is standing in the upper left corner of the grid, which is row 1 and column 1.

Xiaolan can walk on the grid chart. When walking, if he is currently at row r and column c, he cannot go to a row with a row number smaller than r, nor can he go to a column with a column number smaller than c. At the same time, the straight-line distance he walks in one step does not exceed 3.

For example, if Xiaolan is currently at row 3, column 5, he can go to row 3, column 6, row 3, column 7, row 3, column 8, row 4, column 5, row 4 One of row 6, row 4, column 7, row 5, column 5, row 5, column 6, row 6, column 5.

Xiaolan will eventually go to the nth row and the mth column.

In the picture, some positions have rewards, which can be obtained by walking up, and some positions have punishments, which must be accepted by walking up. The reward and punishment are finally abstracted into a weight, the reward is positive, and the punishment is negative.

Xiaolan hopes that after going from row 1, column 1 to row n, column m, the total weight sum is the largest. What is the maximum?

enter description

The first line of input contains two integers n, m, denoting the size of the graph.

The next n lines, each with m integers, represent the weight of each point in the grid.

其中,1≤n≤100,−10**4≤权值≤10**4。

输出描述

输出一个整数,表示最大权值和。

输入输出样例

示例 1

输入

  1. 3 5
  2. -4 -5 -10 -3 1
  3. 7 5 -9 3 -10
  4. 10 -2 6 -10 -4

输出

15

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M

思路:这个题采用动态规划的思想来解答,跟这篇文章上面第四题路径那个题目是差不多的。

做这个题目我认为有两点比较重要。一是想明白初始条件,二是确定状态转移方程。

通过读题,我们得到的题目条件为:如下图,从白点区域可以一步跳到对勾区域。

因此反过来说也就是:对于下面这个图,想要去到白点区域,可以从这些对勾区域一步跳过去。

假设题目所给的各个点的权值存储在w这个二位列表当中,二维列表dp存储的是从第一行第一列那个点走到当前点的最大值。

那么初始条件dp[0][0]=w[0][0]

然后就是确定状态转移方程:dp[i][j]=max(dp[i][j],dp[i-1][j-b])

代码:

  1. n,m=map(int,input().split())
  2. w=[]
  3. for i in range(n):
  4. w.append(list(map(int,input().split())))
  5. dp=[[0 for i in range(m)] for j in range(n)]
  6. dp[0][0]=w[0][0]
  7. for i in range(0,n):
  8. for j in range(0,m):
  9. for a in range(0,4):
  10. for b in range(0,4-a):
  11. if i-a>=0 and j-b>=0:
  12. dp[i][j]=max(dp[i][j],dp[i-a][j-a]+w[i-a][j-a])
  13. print(dp[n-1][m-1])

       这个题的代码我是仿照第四题来写的,第四题的初始条件是前22个数的dp都是直接赋值,1和那个数的最小公倍数。我做这个题也没多想,一开始直接把起点能一步跳过去的位置的dp值全都赋值w了,后来一想不对。人家第四题本身就是经过思考,确定了前22个数的dp就是1和它本身的最小公倍数,这就是最优解,才赋值的。。这个题的w可是有正有负,又没让求步数最小,还没准跳几步才能得到权值最小值呢。

13. 受伤的皇后(2021模拟题)

题目描述

有一个 n×n 的国际象棋棋盘(n 行n 列的方格图),请在棋盘中摆放 n 个受伤的国际象棋皇后,要求:

  1. 任何两个皇后不在同一行。
  2. 任何两个皇后不在同一列。
  3. 如果两个皇后在同一条 45 度角的斜线上,这两个皇后之间行号的差值至少为 3 。

请问一共有多少种摆放方案。

输入描述

输入的第一行包含一个整数 n。

其中,1≤n≤10。

输出描述

输出一个整数,表示答案。

输入输出样例

示例 1

输入

4

输出

2

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M

代码:这个代码我是看的蓝桥官网上的的一个题解代码

  1. n=int(input())
  2. b=0
  3. def queen(A,row=0):
  4. global b
  5. if row==len(A):
  6. b+=1
  7. else:
  8. for wid in range(len(A)):
  9. A[row]=wid
  10. flag=True
  11. for hei in range(row):
  12. if wid==A[hei]:
  13. flag=False
  14. elif abs(hei-row)==abs(wid-A[hei]):
  15. if abs(hei-row)<3:
  16. flag=False
  17. if flag:
  18. queen(A,row+1)
  19. queen([None]*n)
  20. print(b)

思路解析:A是一个含有n个数的一维列表,Ai=a表示第i行的皇后放在这一行第a列的位置。这样在检查皇后排列的时候,检查是否皇后在同一列这个限制条件就特别方便。

14. 删除字符

题目描述

给定一个单词,请问在单词中删除 t个字母后,能得到的字典序最小的单词是什么?

输入描述

输入的第一行包含一个单词,由大写英文字母组成。

第二行包含一个正整数 t。

其中,单词长度不超过 100,t 小于单词长度。

输出描述

输出一个单词,表示答案。

输入输出样例

示例 1

输入

  1. LANQIAO
  2. 3

输出

AIAO

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M

思路:用贪心思想来做,题目让删除t个字母,每次删除一个字母得到当前字典序最小的单词都是下一步删除字母的中间过程。

对于删除哪个字母,对于该单词从前往后遍历,只要左边字母的字典序大于右边字母的字典序,那么就删除这个左边字母。

代码:

  1. s=list(input())
  2. t=int(input())
  3. for j in range(t):
  4. index=0
  5. while s[index]<=s[index+1]:
  6. index=index+1
  7. s.remove(s[index])
  8. print(''.join(s))

15. 相乘

本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。

 小蓝发现,他将 1至 1000000007 之间的不同的数与 2021相乘后再求除以 1000000007 的余数,会得到不同的数。 小蓝想知道,能不能在 1 至 1000000007 之间找到一个数,与 2021 相乘后 再除以 1000000007 后的余数为 999999999。如果存在,请在答案中提交这个数; 如果不存在,请在答案中提交 0。

这个就比较简单,直接暴力求解

代码:

  1. for i in range(1,1000000008):
  2. if i*2021%1000000007==999999999:
  3. print(i)
  4. break

16. 灌溉(模拟赛赛题)

题目描述

小蓝负责花园的灌溉工作。

花园可以看成一个 n 行 m 列的方格图形。中间有一部分位置上安装有出水管。

小蓝可以控制一个按钮同时打开所有的出水管,打开时,有出水管的位置可以被认为已经灌溉好。

每经过一分钟,水就会向四面扩展一个方格,被扩展到的方格可以被认为已经灌溉好。即如果前一分钟某一个方格被灌溉好,则下一分钟它上下左右的四个方格也被灌溉好。

给定花园水管的位置,请问 kk 分钟后,有多少个方格被灌溉好?

输入描述

输入的第一行包含两个整数 n,m。

第二行包含一个整数 t,表示出水管的数量。

接下来 t 行描述出水管的位置,其中第 i 行包含两个数 r,c 表示第r行第c 列有一个排水管。

接下来一行包含一个整数 k。

其中,1≤n,m≤100,1≤t≤10,1≤k≤100。

输出描述

输出一个整数,表示答案。

输入输出样例

示例 1

输入

  1. 3 6
  2. 2
  3. 2 2
  4. 3 4
  5. 1

输出

9

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M

这个题比较简单就是用枚举来做

代码:

  1. n,m=map(int,input().split())
  2. t=int(input())
  3. s=[[0 for i in range(m)] for j in range(n)]
  4. s1=[[0 for i in range(m)] for j in range(n)]
  5. for i in range(t):
  6. x,y=map(int,input().split())
  7. s[x-1][y-1]=1
  8. k=int(input())
  9. for i in range(k):
  10. for a in range(n):
  11. for b in range(m):
  12. if s[a][b]==1:
  13. s1[a][b]=1
  14. if a>0:
  15. s1[a-1][b]=1
  16. if b>0:
  17. s1[a][b-1]=1
  18. if a<n-1:
  19. s1[a+1][b]=1
  20. if b<m-1:
  21. s1[a][b+1]=1
  22. s=s1
  23. print(sum([sum(i) for i in s]))

17. 扫雷

题目描述

在一个 n 行 m 列的方格图上有一些位置有地雷,另外一些位置为空。

请为每个空位置标一个整数,表示周围八个相邻的方格中有多少个地雷。

输入描述

输入的第一行包含两个整数 n,m。

第 2 行到第 n+1 行每行包含 m 个整数,相邻整数之间用一个空格分隔。如果对应的整数为 0,表示这一格没有地雷。如果对应的整数为 1,表示这一格有地雷。

其中,1≤n,m≤100 分钟后还是在当天。

输出描述

输出 n 行,每行 m 个整数,相邻整数之间用空格分隔。

对于没有地雷的方格,输出这格周围的地雷数量。对于有地雷的方格,输出 9。

输入输出样例

示例 1

输入

  1. 3 4
  2. 0 1 0 0
  3. 1 0 1 0
  4. 0 0 1 0

输出

  1. 2 9 2 1
  2. 9 4 9 2
  3. 1 3 9 2

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M

这个题也很简单,直接两层循环就好

  1. a,b=map(int,input().split())
  2. s=[]
  3. for i in range(a):
  4. s.append(list(map(int,input().split())))
  5. ans=[[0 for i in range(b)] for j in range(a)]
  6. for i in range(a):
  7. for j in range(b):
  8. if s[i][j]==1:
  9. ans[i][j]=9
  10. else:
  11. tmp=0
  12. for ii in range(-1,2):
  13. for jj in range(-1,2):
  14. if i+ii>=0 and i+ii<a and j+jj>=0 and j+jj<b:
  15. tmp=tmp+s[i+ii][j+jj]
  16. ans[i][j]=tmp
  17. for i in range(a):
  18. for j in range(b):
  19. print(ans[i][j],end=' ')
  20. print()

18. 双向排序

题目描述

给定序列(a1​,a2​,⋅⋅⋅,an​)=(1,2,⋅⋅⋅,n),即 ai​=i。

小蓝将对这个序列进行 m 次操作,每次可能是将 a1​,a2​,⋯,aqi​​ 降序排列,或者将 aqi​​,aqi+1​​,⋯,an​ 升序排列。

请求出操作完成后的序列。

输入描述

输入的第一行包含两个整数 n,m​,分别表示序列的长度和操作次数。

接下来 m​ 行描述对序列的操作,其中第 i 行包含两个整数 pi​,qi​​ 表示操作类型和参数。当 pi​=0​​ 时,表示将 a1​,a2​,⋅⋅⋅,aqi​​​​ 降序排列;当 pi​=1​ 时,表示将 aqi​​,aqi+1​​,⋯,an​​ 升序排列。

输出描述

输出一行,包含 n 个整数,相邻的整数之间使用一个空格分隔,表示操作完成后的序列。

输入输出样例

示例

输入

  1. 3 3
  2. 0 3
  3. 1 2
  4. 0 2

输出

3 1 2

样例说明

原数列为(1,2,3)​​​​​。

第 1​​​​​ 步后为(3,2,1)​​​​​。

第 2​​​​ 步后为(3,1,2)​​。

第 3​​​ 步后为(3,1,2)​。与第 2 步操作后相同,因为前两个数已经是降序了。

评测用例规模与约定

对于 30% 的评测用例,n,m≤1000;

对于 60% 的评测用例,n,m≤5000;

对于所有评测用例,1≤n,m≤100000,0≤pi​≤1,1≤qi​≤n。

运行限制

  • 最大运行时间:2s
  • 最大运行内存: 256M

这个题我用python本身自带的sort函数进行排序的话,时间会超限

下面是时间超限的代码:

  1. n,m=map(int,input().split())
  2. s=[]
  3. for i in range(m):
  4. p,q=map(int,input().split())
  5. s.append((p,q))
  6. l=[i for i in range(1,n+1)]
  7. for i in range(m):
  8. p,q=s[i]
  9. if p==0:
  10. tmp=l[0:q]
  11. tmp.sort(reverse=True)
  12. l=tmp+l[q:]
  13. else:
  14. tmp=l[q-1:]
  15. tmp.sort()
  16. l=l[0:q-1]+tmp
  17. for i in range(n):
  18. print(l[i],end=' ')

这个题我在蓝桥杯练习系统没看到有人发布python的题解,,先这样。。后续找到了在更新。

19. 谈判

题目描述

在很久很久以前,有 n 个部落居住在平原上,依次编号为 1 到 n。第 i 个部落的人数为 ti​。

有一年发生了灾荒。年轻的政治家小蓝想要说服所有部落一同应对灾荒,他能通过谈判来说服部落进行联合。

每次谈判,小蓝只能邀请两个部落参加,花费的金币数量为两个部落的人数之和,谈判的效果是两个部落联合成一个部落(人数为原来两个部落的人数之和)。

输入描述

输入的第一行包含一个整数 n,表示部落的数量。

第二行包含 n 个正整数,依次表示每个部落的人数。

其中,1≤n≤1000,1≤ti​≤104。

输出描述

输出一个整数,表示最小花费。

输入输出样例

示例 1

输入

  1. 4
  2. 9 1 3 5

输出

31

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M

思路:这个也很简单,每次找人数最少的两个部落进行谈判即可

代码:

  1. n=int(input())
  2. s=list(map(int,input().split()))
  3. result=0
  4. s.sort()
  5. for i in range(n-1):
  6. ans=s[0]+s[1]
  7. result=result+ans
  8. s.pop(0)
  9. s.pop(0)
  10. s.append(ans)
  11. s.sort()
  12. print(result)

20.最少砝码

这个我之前写过题解:

(142条消息) 蓝桥杯备赛:贪心_睡会dd的博客-CSDN博客

21. 距离和

题目描述

本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。

两个字母之间的距离定义为它们在字母表中位置的距离。例如 A 和 C 的距离为 2,L 和 Q 的距离为 5。

对于一个字符串,我们称字符串中两两字符之间的距离之和为字符串的内部距离。

例如:ZOO 的内部距离为 22,其中 Z 和 O 的距离为 11。

请问,LANQIAO 的内部距离是多少?

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M

代码:

  1. s='LANQIAO'
  2. ans=0
  3. for i in range(6):
  4. for j in range(i+1,7):
  5. ans=abs(ord(s[i])-ord(s[j]))+ans
  6. print(ans)

这个题很简单,唯一需要记住的是ord函数以一个字符(长度为1的字符串)作为参数,返回对应的 ASCII 数值,或者 Unicode 数值。

同时提一下chr函数,chr() 用一个范围在 range(256)内的(就是0~255)整数作参数,返回一个对应的字符。

22.时间加法(模拟题)

题目描述

现在时间是 a 点 b 分,请问 t 分钟后,是几点几分?

输入描述

输入的第一行包含一个整数 a。

第二行包含一个整数 b。

第三行包含一个整数 t。

其中0≤a≤23,0≤b≤59,0≤t,t 分钟后还是在当天。

输出描述

输出第一行包含一个整数,表示结果是几点。

第二行包含一个整数,表示结果是几分。

输入输出样例

示例 1

输入

  1. 3
  2. 20
  3. 165

输出

  1. 6
  2. 5

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M

代码:

  1. a=int(input())
  2. b=int(input())
  3. t=int(input())
  4. a=a+(b+t)//60
  5. if a>23:
  6. a=a-24
  7. b=(b+t)%60
  8. print(a)
  9. print(b)

23. 排列小球

题目描述

小蓝有黄绿蓝三种颜色的小球,分别为 R,G,B 个。同样颜色的小球没有区别。

小蓝将这些小球从左到右排成一排,排完后,将最左边的连续同色小球个数记为 t1​,将接下来的连续小球个数记为 t2​,以此类推直到最右边的小球。

请问,总共有多少总摆放小球的方案,使得 t1​,t2​,⋯ 为严格单调递增序列,即 t1​≤t2​≤t3​≤⋯。

输入描述

输入一行包含三个整数 R,G,B。

其中,0≤R,G,B≤50。。

输出描述

输出一个整数,表示答案。

输入输出样例

示例 1

输入

3 6 0

输出

3

样例说明

用 r 表示红球,g 表示绿球,可能的方案包括:

rrrgggggg

grrrggggg

ggrrrgggg

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M

代码:这个是看的蓝桥官网别人发的

  1. r,g,b=[int(x)for x in input().split(' ')]
  2. S=0
  3. def dfs(r,g,b,i,c):
  4. global S
  5. if r==0 and g==0 and b==0:
  6. S+=1
  7. return True
  8. if r>i and c!='r':
  9. for j in range(i,r+1):
  10. if j>i:
  11. dfs(r-j,g,b,j,'r')
  12. if g>i and c!='g':
  13. for j in range(i,g+1):
  14. if j>i:
  15. dfs(r,g-j,b,j,'g')
  16. if b>i and c!='b':
  17. for j in range(i,b+1):
  18. if j>i:
  19. dfs(r,g,b-j,j,'b')
  20. return False
  21. dfs(r,g,b,0,'')
  22. print(S)

24. 序列个数

题目描述

本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。

请问有多少个序列满足下面的条件:

  1. 序列的长度为 5。
  2. 序列中的每个数都是 1 到 10 之间的整数。
  3. 序列中后面的数大于等于前面的数。

代码:

  1. count=0
  2. for a in range(1,11):
  3. for b in range(a,11):
  4. for c in range(b,11):
  5. for d in range(c,11):
  6. for e in range(d,11):
  7. count+=1
  8. print(count)

答案:2002

25. 合法日期

题目描述

小蓝正在上小学,老师要求同学们在暑假每天记日记。可是小蓝整个暑假都在玩,直到最后一天才想起要记日记。于是小蓝赶紧编了一些日记交给老师。

没想到,日记很快就被老师发现了问题,原来小蓝记完 8 月 31 日的日记,竟又记了 8 月 32 日和 8 月 33 日的日记。这显然是有问题的,因为根本没有 8 月 32 日和 8 月 33 日。

给定一个月份和一个日期,请问 2021 年有没有这一天。

输入描述

输入的第一行包含一个整数 m,表示月份。

第二行包含一个整数 d,表示日期。

其中,1≤m≤20,1≤d≤40。

输出描述

如果2021年有 m 月 d 日,输入 yes,否则输出 no

输入输出样例

示例 1

输入

  1. 8
  2. 32

输出

no

示例 2

输入

  1. 2
  2. 28

输出

yes

代码:

  1. import datetime
  2. m=int(input())
  3. d=int(input())
  4. try:
  5. a=datetime.date(2021,m,d)
  6. print('yes')
  7. except:
  8. print('no')

这次用到了datetime模块,把这个模块的其他函数也学习一下:

Python datetime模块详解 - Awakenedy - 博客园 (cnblogs.com)

26. 图像模糊

题目描述

小蓝有一张黑白图像,由 n×m 个像素组成,其中从上到下共 n 行,每行从左到右 m 列。每个像素由一个 0 到 255 之间的灰度值表示。

现在,小蓝准备对图像进行模糊操作,操作的方法为:

对于每个像素,将以它为中心 3×3 区域内的所有像素(可能是 9 个像素或少于 9 个像素)求和后除以这个范围内的像素个数(取下整),得到的值就是模糊后的结果。

请注意每个像素都要用原图中的灰度值计算求和。

输入描述

输入的第一行包含两个整数 n,m。

第 2 行到第n+1 行每行包含 m 个整数,表示每个像素的灰度值,相邻整数之间用一个空格分隔。

其中,1≤n,m≤100 。

输出描述

输出 n 行,每行 m 个整数,相邻整数之间用空格分隔,表示模糊后的图像。

输入输出样例

示例 1

输入

  1. 3 4
  2. 0 0 0 255
  3. 0 0 255 0
  4. 0 30 255 255

输出

  1. 0 42 85 127
  2. 5 60 116 170
  3. 7 90 132 191

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M

代码:

  1. import math
  2. n,m=map(int,input().split())
  3. s=[]
  4. for i in range(n):
  5. s.append(list(map(int,input().split())))
  6. s1=[[0 for i in range(m)] for j in range(n)]
  7. for i in range(n):
  8. for j in range(m):
  9. count=1
  10. tmp=s[i][j]
  11. if i-1>=0 and j-1>=0:
  12. count=count+1
  13. tmp=tmp+s[i-1][j-1]
  14. if i-1>=0 and j>=0:
  15. count=count+1
  16. tmp=tmp+s[i-1][j]
  17. if i-1>=0 and j+1<=m-1:
  18. count=count+1
  19. tmp=tmp+s[i-1][j+1]
  20. if j-1>=0:
  21. count=count+1
  22. tmp=tmp+s[i][j-1]
  23. if j+1<=m-1:
  24. count=count+1
  25. tmp=tmp+s[i][j+1]
  26. if i+1<=n-1 and j-1>=0:
  27. count=count+1
  28. tmp=tmp+s[i+1][j-1]
  29. if i+1<=n-1 :
  30. count=count+1
  31. tmp=tmp+s[i+1][j]
  32. if i+1<=n-1 and j+1<=m-1:
  33. count=count+1
  34. tmp=tmp+s[i+1][j+1]
  35. s1[i][j]=math.floor(tmp/count)
  36. for i in range(n):
  37. for j in range(m):
  38. print(s1[i][j],end=' ')
  39. print()

python取整函数:

向上取整:math.ceil()

四舍五入:round()

向下取整:math.floor()

向0取整:int()

27. 数字位数

题目描述

本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。

整数 1 到 6 连在一起,成为 123456,长度为 6。

整数 1 到 12 连在一起,成为 123456789101112,长度为 15。

请问整数 1 到 2020 连在一起,长度为多少?

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M
  1. count=0
  2. for i in range(1,2021):
  3. count=count+len(str(i))
  4. print(count)

28. 时间显示

代码:

  1. from datetime import datetime,timedelta
  2. start=datetime(year=1970,month=1,day=1)
  3. dela=timedelta(milliseconds=1)
  4. now=int(input())
  5. now=start+now*dela
  6. print("%02d:%02d:%02d"%(now.hour,now.minute,now.second))

 



Category of website: technical article > Blog

Author:python98k

link:http://www.pythonblackhole.com/blog/article/327/191727f62a2e1fdcbaa3/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

6 0
collect article
collected

Comment content: (supports up to 255 characters)