当前位置:   article > 正文

【蓝桥杯真题】29天备战 Python讲解_python蓝桥杯备战路线

python蓝桥杯备战路线

距离蓝桥杯还有29天 下面对执梗大哥给出的每日训练作出分析——所有题目均可在蓝桥官网找到

 1.奇数倍数

  1. def judge(x):
  2. p=str(x)
  3. for i in p:
  4. if int(i)%2==0:
  5. return False
  6. return True
  7. i=2019
  8. while True:
  9. if judge(i):
  10. print(i)
  11. break
  12. else:
  13. i+=2019

比较简单 遍历每一位 判断能否整除2 不能就是奇数 


 2.第几个幸运数字

  1. s=[]
  2. for i in range(100):
  3. for j in range(100):
  4. for k in range(100):
  5. s.append(3**i*5**j*7**k)
  6. s.sort()
  7. print(s.index(59084709587505))

因为只有三种因子,不妨假设n=3^i*5^j*7^k 三重循环枚举所有可能即可  非常简单

3.四数平方和 

  1. n=int(input())
  2. l=[0]*5000000
  3. for i in range(0,int(n**0.5)+1):
  4. for j in range(i,int(n**0.5)+1):
  5. if i**2+j**2<=n:
  6. l[i**2+j**2]=1
  7. def f(n):
  8. for i in range(0,int(n**0.5)+1):
  9. for j in range(i,int(n**0.5)+1):
  10. if l[n-i**2-j**2]==1:#tmp=n-(i**2+j**2)存在(i**2+j**2递增)
  11. tmp=n-i**2-j**2#待寻找的两数平方和
  12. for k in range(j,int(n**0.5)+1):#i<=j<=k<=p
  13. p=int((tmp-k**2)**0.5)**2#待判断数
  14. if p==tmp-k**2:#判断是否是完全平方数,右侧是目标数字的平方,左边是待判断数
  15. print(i,j,k,int(p**0.5))
  16. return 0
  17. f(n)

没有AC,75分,思路和cpp一样,但没过去,如果有更好的办法,恳请指导。下面对这种思路进行讲解,在cpp应该是能过的哈。假设n=x^2+y^2+z^2+p^2 

先考虑两数的平方之和 把x^2+y^2<=n所有满足的整数解 记录下来 表示为1

其余表示为0的无非是其平方和>n或者无法用平方和表示出来(比如3就没法用两数平方和表示),不在考虑范围内

等于说现在我们已经有了一个数字 这个数字<=n,并可以用两数平方和表示

题目要求四数平方和,我们只需要在刚刚我们划分为1的数据内继续找两个

4.迷宫 

  1. s="""01010101001011001001010110010110100100001000101010
  2. 00001000100000101010010000100000001001100110100101
  3. 01111011010010001000001101001011100011000000010000
  4. 01000000001010100011010000101000001010101011001011
  5. 00011111000000101000010010100010100000101100000000
  6. 11001000110101000010101100011010011010101011110111
  7. 00011011010101001001001010000001000101001110000000
  8. 10100000101000100110101010111110011000010000111010
  9. 00111000001010100001100010000001000101001100001001
  10. 11000110100001110010001001010101010101010001101000
  11. 00010000100100000101001010101110100010101010000101
  12. 11100100101001001000010000010101010100100100010100
  13. 00000010000000101011001111010001100000101010100011
  14. 10101010011100001000011000010110011110110100001000
  15. 10101010100001101010100101000010100000111011101001
  16. 10000000101100010000101100101101001011100000000100
  17. 10101001000000010100100001000100000100011110101001
  18. 00101001010101101001010100011010101101110000110101
  19. 11001010000100001100000010100101000001000111000010
  20. 00001000110000110101101000000100101001001000011101
  21. 10100101000101000000001110110010110101101010100001
  22. 00101000010000110101010000100010001001000100010101
  23. 10100001000110010001000010101001010101011111010010
  24. 00000100101000000110010100101001000001000000000010
  25. 11010000001001110111001001000011101001011011101000
  26. 00000110100010001000100000001000011101000000110011
  27. 10101000101000100010001111100010101001010000001000
  28. 10000010100101001010110000000100101010001011101000
  29. 00111100001000010000000110111000000001000000001011
  30. 10000001100111010111010001000110111010101101111000"""
  31. maze=s.split('\n')#30行50列 maze[n][m] 0<=n<=29,0<=m<=49
  32. dy=[0,-1,1,0]
  33. dx=[1,0,0,-1]
  34. def judge(x,y):
  35. global parent,maze
  36. if 0<=x<=29 and 0<=y<=49 and maze[x][y]=='0' and (x,y) not in parent.keys():
  37. return True
  38. return False
  39. parent={(0,0):(0,0)}
  40. endx,endy=29,49
  41. searched=[(0,0)]
  42. def g(x1,y1,x2,y2):
  43. if x2-x1>0:
  44. return 'D'
  45. if x2-x1<0:
  46. return 'U'
  47. if y2-y1>0:
  48. return 'R'
  49. if y2-y1<0:
  50. return 'L'
  51. while searched:
  52. now=searched.pop(0)
  53. x,y=now[0],now[1]
  54. if x==endx and y==endy:
  55. end=(x,y)
  56. start=(0,0)
  57. t=''
  58. while parent[end]!=start:
  59. print(end)
  60. t=g(parent[end][0],parent[end][1],end[0],end[1])+t
  61. end=parent[end]
  62. t=g(parent[end][0],parent[end][1],end[0],end[1])+t
  63. print(t)
  64. break
  65. else:
  66. for i in range(4):
  67. nx,ny=x+dx[i],y+dy[i]
  68. if judge(nx,ny):
  69. parent[(nx,ny)]=(x,y)
  70. searched.append((nx,ny))

 利用广度优先搜索 同时要记录路径 需要一个字典来记录前驱结点 每次出队的元素

对其合法邻居入队 何为合法邻居:在地图内,不是障碍物,没有创建过前驱关系(一个结点只能有一个parent)


有任何疑惑和建议欢迎提出 希望大家都能在这次蓝桥杯省赛中拿到省一!!! 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/64264
推荐阅读
相关标签
  

闽ICP备14008679号