【思特奇杯·云上蓝桥-算法集训营】第2周

1.带分数

a = input()
a_num = int(a)
count = 0
for i in range(int('9'*(5-len(a))), int('9'*(6-len(a)))):
    for n in range(1, a_num):
        stra = str(n) + str((a_num - n)*i) + str(i)
        for j in range(9):
            if str(j+1) not in stra:
                j = 7
                break
        if j == 8 and len(stra) == 9:
            count += 1
print(count)

2.李白打酒

num = 0
def count(a, b, c):
    global num
    if a > 0:
        count(a - 1, b, c * 2)
    if b > 0:
        count(a, b - 1, c - 1)
    if a == 0 and b == 0 and c == 1:
        num += 1
count(5, 9, 2)
print(num)

3.第39级台阶

dp = [[0]*2 for row in range(40)]
dp[1][0] = 0
dp[1][1] = 1
dp[2][0] = 1
dp[2][1] = 1
for i in range(3, 40):
    dp[i][0] = dp[i - 1][1] + dp[i - 2][1]
    dp[i][1] = dp[i - 1][0] + dp[i - 2][0]
print(dp[39][0])

6.跳马

a, b, c, d = map(int, input().split(' '))
step = [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)]
visit = [[False]*8 for _ in range(8)]
queue = [(a, b, 0)] 
while queue:
    y, x, t = queue.pop(0)

    if y == c and x == d:
        print(t)
        break

    for dy, dx in step:
        ny = y + dy
        nx = x + dx
        if -1 < ny < 8 and -1 < nx < 8 and not visit[ny][nx]:
            queue.append((ny, nx, t+1))
            visit[ny][nx] = True
if not queue:
    print(-1)

8.未名湖边的烦恼

m, n = map(int, input().split(' '))
record = [[-1]*(n+1) for _ in range(m+1)]
def dfs(a, b): # 还鞋、借鞋
    if a == b == 0:
        return 1
    
    if record[a][b] != -1:
        ans = record[a][b]
    else:
        ans = 0
        if a > 0:
            ans += dfs(a-1, b)
        if 0 < b <= a:
            ans += dfs(a, b-1)
        record[a][b] = ans
    return ans
print(dfs(m, n))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值