当前位置:   article > 正文

第十届蓝桥杯 省赛研究生组 真题解析(Python)_蓝桥杯python真题及解答

蓝桥杯python真题及解答

试题A:立方和(5分)

答案:4097482414389

解析:代码如下

  1. def judge(num):
  2. string = str(num)
  3. if( '2' in string or '0' in string or '1' in string or '9' in string):
  4. return True
  5. return False
  6. if(__name__=='__main__'):
  7. cnt = 0
  8. for a in range(1,2020):
  9. if(judge(a)):
  10. cnt+=(a*a*a)
  11. print(cnt)

试题B:字串数字(5分)

答案:3725573269

解析:将其看成26进制的数,代码如下

  1. if(__name__=='__main__'):
  2. num = 0
  3. string = 'LANQIAO'
  4. jw = 0
  5. for i in range(0,len(string)):
  6. num=(ord(string[i])-ord('A')+1+num*26)
  7. jw+=1
  8. print(num)

试题C:质数(10分)

答案:17569

解析:代码如下

  1. import math
  2. def is_prime(num):
  3. if(num>3):
  4. for i in range(2,int(math.sqrt(num)+1)):
  5. if(num%i==0):
  6. return False
  7. return True
  8. if(__name__=='__main__'):
  9. cnt = 0
  10. num = 2
  11. while(True):
  12. if(is_prime(num)):
  13. cnt+=1
  14. if(cnt==2019):
  15. print(num)
  16. break
  17. num+=1

试题D:最短路(10分)

答案:6

解析:不需要代码,直接自己用最短路手动找还快一点,我随意找了三条路,有错误请指出

试题E:RSA解密(15分)

​​​​​​​

答案:579706994112328949

试题F:Fibonacci数列与黄金分割(15分)

解析:题目有提示,当n足够大时会趋近黄金分割,自己测试一下找到黄金分割的阈值,代码如下

  1. if(__name__=='__main__'):
  2. f = [1,1]
  3. index = 1
  4. n = int(input())
  5. if( n == 1 ):
  6. print(1)
  7. elif( n == 2 ):
  8. print('0.50000000')
  9. elif( n > 30 ):
  10. print('0.61803399')
  11. else:
  12. for i in range(2,n+1) :
  13. tmp = f[1]+f[0]
  14. index = (index+1)%2
  15. f[index] = tmp
  16. print('%.8lf' % (f[index]/(f[1]+f[0])))

试题G:扫地机器人

​​​​​​​

解析:我的答案有点问题,但我找不到问题在哪,有大佬请帮我指出

  1. def get_l(nums,n):
  2. l = 1
  3. while(True):
  4. res = judge(nums,l)
  5. if(res):
  6. return l
  7. l += 1
  8. def judge(nums,l):
  9. sweep = 0
  10. for i in range(len(nums)):
  11. it = nums[i]
  12. if(it-l<=sweep):
  13. if(it<=sweep):
  14. sweep=it+l-1
  15. else:
  16. sweep+=l
  17. else:
  18. return False
  19. return True
  20. if(__name__=='__main__'):
  21. nks = input().split()
  22. n = int(nks[0])
  23. k = int(nks[1])
  24. nums = []
  25. l=n
  26. for i in range(k):
  27. nums.append(int(input()))
  28. nums.sort()
  29. l = 0
  30. r = n
  31. ans = n
  32. while(l<=r):
  33. m = int((l+r) // 2)
  34. res = judge(nums,m)
  35. if(res):
  36. r = m-1
  37. ans = m
  38. else:
  39. l = m+1
  40. print(2*(ans-1))

试题H:修改数组(20分)

答案:代码如下

  1. def get_next(num,l):
  2. while(num in l):
  3. num += 1
  4. return num
  5. if(__name__=='__main__'):
  6. s_l = set()
  7. n = int(input())
  8. l = input().split()
  9. vis = [0 for i in range(1111111)]
  10. for i in range(len(l)):
  11. if(not i==0):
  12. print(' ',end = '')
  13. num = int(l[i])
  14. while(vis[num]==1):
  15. num += 1
  16. vis[num]=1
  17. s_l.add(num)
  18. print(num,end = '')

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

闽ICP备14008679号