蓝桥·算法赛——第08场

小白入门赛

1-坤星球

2-二进制王国

from functools import cmp_to_key

def cmp(s1, s2):
    return -1 if s1 + s2 <= s2 + s1 else 1
n = int(input())
a = sorted([input() for _ in range(n)], key=cmp_to_key(cmp))
print("".join(a))

3-djwcb

T = int(input())
for _ in range(T):
    x, p = map(str, input().split()); x = int(x) % 10
    if x == 0 or x == 1 or x == 5 or x == 6:
        print(x)
    elif x == 4:
        print(4 if int(p[-1]) % 2 != 0 else 6)
    elif x == 9:
        print(9 if int(p[-1]) % 2 != 0 else 1)
    else:
        p = int(p[-2:]) % 4
        if x == 2:
            print([6, 2, 4, 8][p])
        elif x == 3:
            print([1, 3, 9, 7][p])
        elif x == 7:
            print([1, 7, 9, 3][p])
        else:
            print([6, 8, 4, 2][p])

4-求解线性方程组

def calc():
    for i in range(3, n + 1):
        x[i] = a[i - 1] - x[i - 1] - x[i - 2]
        if x[i] != 0 and x[i] != 1: # 提前判定
            return False
    return True

n = int(input())
a = [0] + list(map(int, input().split()))

x = [0] * (n + 1) # x[i] 代表解
if a[1] == 0:
    x[1], x[2] = 0, 0
elif a[1] == 1:
    x[1], x[2] = 0, 1
else:
    x[1], x[2] = 1, 1
if n == 2: # 特殊情况 虽然有两个方程但方程相同
    print(*x[1 : ])
else: # n个方程n个未知数
    if calc(): # 计算成功
        print(*x[1 : ])
    else:
        x[1], x[2] = 1, 0
        calc(); print(*x[1 : ])

5-美丽圆环

T = int(input())
for _ in range(T):
    N = int(input())
    A = [0] + sorted(list(map(int, input().split())))
    # 需要单独处理 N = 2 的情况,否则下面会越界
    if N == 2:
        print(int(A[1] != A[2]))
        continue
    # 保证首尾均有连续的数字
    ans = (A[1] != A[2]) + (A[N-1] != A[N])
    if ans == 2 and (A[2] == A[3] or A[N - 2] == A[N - 1]):
        # 首位均无连续的数字 除第i位和最后一位首尾有连续
        ans = 1 # 看情况将首改尾交换(或尾改首)就保证首尾均有连续的数字
    print(ans)

6-小蓝的跳跃

t = int(input())
for _ in range(t):
    n, x = map(int, input().split())
    a = [0] + list(map(int, input().split())) + [0]
    # 计算在当前情况下心情的最小值 dp1 和最大值 dp2
    dp1 = [0] * (n + 2); dp2 = [0] * (n + 2)
    dp1[1] = dp2[1] = a[1] # 完成初始化
    for i in range(2, n + 2): # 需要更新到 n + 1 的位置
        dp1[i] = min(dp1[i - 1], dp1[i - 2]) + a[i]
        dp2[i] = max(dp2[i - 1], dp2[i - 2]) + a[i]
    print("Yes" if dp1[n + 1] <= x <= dp2[n + 1] else "No")

强者挑战赛

1-求解线性方程组

def calc():
    for i in range(3, n + 1):
        x[i] = a[i - 1] - x[i - 1] - x[i - 2]
        if x[i] != 0 and x[i] != 1: # 提前判定
            return False
    return True

n = int(input())
a = [0] + list(map(int, input().split()))

x = [0] * (n + 1) # x[i] 代表解
if a[1] == 0:
    x[1], x[2] = 0, 0
elif a[1] == 1:
    x[1], x[2] = 0, 1
else:
    x[1], x[2] = 1, 1
if n == 2: # 特殊情况 虽然有两个方程但方程相同
    print(*x[1 : ])
else: # n个方程n个未知数
    if calc(): # 计算成功
        print(*x[1 : ])
    else:
        x[1], x[2] = 1, 0
        calc(); print(*x[1 : ])

2-美丽圆环

T = int(input())
for _ in range(T):
    N = int(input())
    A = [0] + sorted(list(map(int, input().split())))
    # 需要单独处理 N = 2 的情况,否则下面会越界
    if N == 2:
        print(int(A[1] != A[2]))
        continue
    # 保证首尾均有连续的数字
    ans = (A[1] != A[2]) + (A[N-1] != A[N])
    if ans == 2 and (A[2] == A[3] or A[N - 2] == A[N - 1]):
        # 首位均无连续的数字 除第i位和最后一位首尾有连续
        ans = 1 # 看情况将首改尾交换(或尾改首)就保证首尾均有连续的数字
    print(ans)

3-小蓝的跳跃

t = int(input())
for _ in range(t):
    n, x = map(int, input().split())
    a = [0] + list(map(int, input().split())) + [0]
    # 计算在当前情况下心情的最小值 dp1 和最大值 dp2
    dp1 = [0] * (n + 2); dp2 = [0] * (n + 2)
    dp1[1] = dp2[1] = a[1] # 完成初始化
    for i in range(2, n + 2): # 需要更新到 n + 1 的位置
        dp1[i] = min(dp1[i - 1], dp1[i - 2]) + a[i]
        dp2[i] = max(dp2[i - 1], dp2[i - 2]) + a[i]
    print("Yes" if dp1[n + 1] <= x <= dp2[n + 1] else "No")

4-赛博奴隶主

5-取气球

6-最后的加k

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胆怯与勇敢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值