Python算法学习: 计蒜客 2020 蓝桥杯大学 B 组省赛模拟赛 (一)题目及解析

A. 结果填空: 有趣的数字

def solve(n):
    tmp = 2
    if tmp == n:
        return True
    while n > tmp:
        k = n % tmp
        if k==0:
            return False
        else:
            tmp += 1
    return True

if __name__ == '__main__':
    count = 0
    for i in range(1, 100000):
        if solve(i) and '5' in str(i):
            count += 1
    print(count)

B. 结果填空:爬楼梯

'''
蒜头要爬楼梯。楼梯一共有 10 层台阶。因为腿长的限制,每次最多能上 4 层台阶。但是第 5,7层楼梯坏掉了不能踩。求上楼梯的方案数
https://nanti.jisuanke.com/t/43116
'''
ans = [0]*11 # 定义存放数据的列表
ans[0] = 1 # 初始化
for i in range(1, 11):
    if i == 5 or i == 7:
        continue
    if i - 1 >= 0:
        ans[i] += ans[i-1]
    if i - 2 >= 0:
        ans[i] += ans[i-2]
    if i - 3 >= 0:
        ans[i] += ans[i-3]
    if i - 4 >= 0:
        ans[i] += ans[i-4]
print(ans)

C. 结果填空:七巧板

'''
思路:

跟平面划分一个道理,每次都会多划分一个区域,F(n)=F(n-1)+1 。
答案是47 = 7+ (6+7+8+9+10)
'''
x = 7
a = 6

for i in range(1, 6):
    x += a
    a += 1
    

print(x)

D. 结果填空:苹果

count = 0
list = [7 ,2 ,12, 5, 9, 9, 8, 10, 7, 10 ,5 ,4 ,5, 8, 4, 4, 10, 11, 3 ,8 ,7 ,8, 3, 2, 1, 6, 3, 9, 7, 1,0,0]
for i in range(30):
    if list[i] >=  3:
        count += list[i] // 3
        list[i] = list[i] % 3
    flag = True
    while flag:
        if list[i] >=1 and list[i + 1] >= 1 and list[i + 2] >= 1:
            count += 1
            list[i] -= 1
            list[i + 1] -= 1
            list[i + 2] -= 1
        else:
            flag = False
print(list)
print(count)

F. 程序设计:寻找重复项

a, b, c = map(int, input().split())
list = []
list.append(1)
for i in range(1, 2000001):
    num = ((a*list[i-1]) + (list[i-1] % b)) % c
    list.append(num)
    if len(list) != len(set(list)):
        print(i)
        break
    else:
        continue

else:
    print(-1)

来源: https://blog.csdn.net/qq_43442524/article/details/104432258