自学内容网 自学内容网

洛谷P5723、P5728、P1428、P1319 Python解析

P5723

完整代码

def is_prime(y):
    if y < 2:
        return False
    for i in range(2, int(y**0.5) + 1):
        if y % i == 0:
            return False
    return True

n = int(input())
sum_primes = 0
x = 0

if n < 2:
    print("0")
elif n == 2:
    print("2\n1")
else:
    for i in range(2, n + 1):
        if i % 2 == 0 and i != 2:
            continue
        if sum_primes + i > n:
            print(x)
            break
        if is_prime(i):
            print(i)
            sum_primes += i
            x += 1

P5728

完整代码

def f(x, y):  # 判断上下各门之差
    return abs(x - y)

n = int(input())
s = []

for i in range(n):
    c, m, e = map(int, input().split())
    total_sum = c + m + e  # 计算每个人的分数和
    s.append({'c': c, 'm': m, 'e': e, 'sum': total_sum})

ans = 0
for i in range(n):
    for j in range(i + 1, n):
        if (f(s[i]['c'], s[j]['c']) <= 5 and
            f(s[i]['e'], s[j]['e']) <= 5 and
            f(s[i]['m'], s[j]['m']) <= 5 and
            f(s[i]['sum'], s[j]['sum']) <= 10):  # 符合条件,ans++
            ans += 1

print(ans)

P1428

完整代码

n = int(input())
a = list(map(int, input().split()))
sum_arr = [0] * n  # 初始化sum数组为0

for i in range(1, n):
    for j in range(i, 0, -1):
        if a[i] > a[j - 1]:
            sum_arr[i] += 1

print(' '.join(map(str, sum_arr)))  # 输出

P1319

完整代码

# 获取输入
inputs = list(map(int, input().strip().split()))

# 提取矩阵大小
n = inputs[0]
compressed = inputs[1:]

result = ""

# 开始解压
current_char = "0"
for count in compressed:
    result += current_char * count
    # 切换当前字符
    current_char = "1" if current_char == "0" else "0"

# 将结果输出为的矩阵
for i in range(n):
    print(result[i * n:(i + 1) * n])


原文地址:https://blog.csdn.net/weixin_56619527/article/details/142699711

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!