站点图标 AI技术聚合

CCFCSP认证历年第一题(Python版汇总)

目录


202305-1 重复局面

n = int(input())
# 创建三维数组 共k个  大小i * j
matrix = [[[0 for i in range(8)] for j in range(8)] for k in range(n)]
for i in range(n):
    for j in range(8):
        Str = input()
        for k in range(8):
            matrix[i][j][k] = Str[k]


# 将三维数组(8,8,8)压缩为二维数组(8,64)
def compress_array(array):
    compressed = [element for sublist in array for subsublist in sublist for element in subsublist]
    reshaped = [compressed[i:i + 64] for i in range(0, len(compressed), 64)]
    return reshaped


Mat = compress_array(matrix)
s = 1
cnt = [1]
for i in range(1, n):
    for j in range(i):
        for k in range(64):
            if Mat[i][k] != Mat[j][k]:
                break
        if k == 63:
            s = s + 1
    cnt.append(s)
    s = 1

for i in cnt:
    print(i)

运行结果:

 

 202303-1 田地丈量

n, a, b = map(int, input().split())
matrix = [[i for i in map(int, input().split())] for j in range(n)]
for i in range(n):
    for j in range(4):
        if matrix[i][j] < 0:
            matrix[i][j] = 0
        if j == 0 or j == 2:
            if matrix[i][j] > a:
                matrix[i][j] = a
        elif j == 1 or j == 3:
            if matrix[i][j] > b:
                matrix[i][j] = b
s = 0
for i in range(n):
    s = s + ((matrix[i][2] - matrix[i][0]) * (matrix[i][3] - matrix[i][1]))
print(s)

运行结果:

 

202212-1 现值计算

n, per = input().split()
n = int(n)
per = float(per)
dig = input().split()
res = 0
for i in range(n + 1):
    dig[i] = int(dig[i])
    res = res + (dig[i] * ((1 + per) ** (-i)))

print(res)

运行结果:

 

202209-1 如此编码

n, m = map(int, input().split())
a = input().split()
for i in range(n):
    a[i] = int(a[i])

# 创建C数组 C0 = 1
C = [1]

for i in range(1, n + 1):
    C.append(a[i - 1] * C[i - 1])

# 求m%ci数组
M_Ci = []
for i in range(1, n + 1):
    M_Ci.append(m % C[i])

# 求m%ci - m%ci-1数组
m_Ci = [M_Ci[0]]
for i in range(1, n):
    m_Ci.append(M_Ci[i] - M_Ci[i - 1])

# 求res结果数组
res = []
for i in range(n):
    res.append(m_Ci[i] // C[i])

for i in res:
    print(i, end=' ')

运行结果:

 

202206-1 归一化处理

n = int(input())
dig = input().split()
for i in range(n):
    dig[i] = int(dig[i])

s = sum(dig)
ave = s / n
# 创建方差小列表
Di = [(i - ave) ** 2 for i in dig]
d = (sum(Di) / n) ** 0.5

# 创建res列表
res = [(i - ave) / d for i in dig]
for i in res:
    print(i)

运行结果:

 

 202203-1 未初始化警告

# 这道题容易超时
n, k = map(int, input().split())
# 创建二维数组
matrix = [[i for i in map(int, input().split())] for j in range(k)]

if matrix[0][1] == 0:
    cnt = 0
else:
    cnt = 1

# 创建辅助集合
assist = set()
assist.add(matrix[0][0])
for i in range(1, k):
    if matrix[i][1] != 0:
        if matrix[i][1] not in assist:
            cnt = cnt + 1
    assist.add(matrix[i][0])
print(cnt)

运行结果:

 

 202112-1 序列查询

n, N = map(int, input().split())
dig = input().split()
for i in range(n):
    dig[i] = int(dig[i])

# 计算差值列表
div = []
for i in range(n - 1):
    div.append(dig[i + 1] - dig[i])
div.append(N - dig[-1])
cnt = [1]
for i in range(1, n):
    Cnt = cnt[i - 1] + 1
    cnt.append(Cnt)
# 计算结果列表
res = [div[i] * cnt[i] for i in range(n)]
print(sum(res))

运行结果:

 

202109-1 数组推导

n = int(input())
dig = input().split()

for i in range(n):
    dig[i] = int(dig[i])
max = sum(dig)
min = sum(set(dig))
print(max)
print(min)

运行结果:

 

202104-1 灰度直方图

n, m, L = map(int, input().split())
# 创建二维数组
matrix = [[i for i in map(int, input().split())] for j in range(n)]
# 将二维数组压缩为一维数组
Mat = []
for i in range(n):
    for j in range(m):
        Mat.append(matrix[i][j])
# 创建L的一维数组
l = [0 for i in range(L)]

for i in range(len(Mat)):
    if 0 <= Mat[i] < L:
        l[Mat[i]] = l[Mat[i]] + 1
for i in l:
    print(i, end=' ')

运行结果:

 

 202012-1 期末预测之安全指数

n = int(input())
matrix = [[i for i in map(int, input().split())] for j in range(n)]
weight_score = []
for i in range(n):
    weight_score.append(matrix[i][0] * matrix[i][1])
res = max(0, sum(weight_score))
print(res)

运行结果:

 

 

202009-1 称检测点查询

n, X, Y = map(int, input().split())
matrix = [[i for i in map(int, input().split())] for j in range(n)]
distance = []
for i in range(n):
    distance.append((matrix[i][0] - X) ** 2 + (matrix[i][1] - Y) ** 2)
Dis = [(i, distance[i]) for i in range(n)]


def fun(li):
    return li[1]


Dis.sort(key=fun)
print(Dis[0][0] + 1, Dis[1][0] + 1, Dis[2][0] + 1, sep='\n')

运行结果:

 

 

202006-1 线性分类器

n, m = map(int, input().split())
matrix = [[i for i in input().split()] for j in range(n)]
Mat = [[i for i in map(int, input().split())] for k in range(m)]
for i in range(n):
    for j in range(2):
        matrix[i][j] = int(matrix[i][j])
res = []
for j in range(m):
    for i in range(n):
        res.append(Mat[j][0]+matrix[i][0]*Mat[j][1]+matrix[i][1]*Mat[j][2])
Res = []
cnt = 0
for i in range(len(res) // n):
    Res.append([])
    for j in range(n):
        Res[i].append(res[j + cnt * n])
    cnt = cnt + 1
alpha = []
for i in range(n):
    if matrix[i][2] == 'A':
        alpha.append(-1)
    else:
        alpha.append(1)
res_ = []
_cnt = 0
cnt_ = 0
for i in range(len(res) // n):
    for j in range(n):
        if alpha[j] * Res[i][j] > 0:
            _cnt = _cnt + 1
        else:
            cnt_ = cnt_ + 1
    if (_cnt == n and cnt_ == 0) or (_cnt == 0 and cnt_ == n):
        res_.append('Yes')
    else:
        res_.append('No')
    _cnt = 0
    cnt_ = 0
for i in res_:
    print(i)




运行结果:

 

 

 

201912-1 报数

cnt = [0, 0, 0, 0]
jump = 0
n = int(input())
count = 0


# 判断7是否在这个数字内
def judge(n):
    a = n // 10
    b = n % 10
    k = a
    lst = [b]
    while a != 0:
        a = k // 10
        b = k % 10
        k = a
        lst.append(b)
    if 7 in lst:
        x = 1
    else:
        x = 0
    return x

判断这个数字是否该跳过
def Jump(n):
    flag = 0
    if n % 7 == 0 or judge(n) == 1:
        flag = 1
    return flag


while count - jump != n:
    count = count + 1
    a = count
    if Jump(a) == 1:
        jump = jump + 1
        cnt[0] = cnt[0] + 1
    # 确保及时跳出 以下类似
    if count - jump == n:
        break
    count = count + 1
    b = count
    if Jump(b) == 1:
        jump = jump + 1
        cnt[1] = cnt[1] + 1
    if count - jump == n:
        break
    count = count + 1
    c = count
    if Jump(c) == 1:
        jump = jump + 1
        cnt[2] = cnt[2] + 1
    if count - jump == n:
        break
    count = count + 1
    d = count
    if Jump(d) == 1:
        jump = jump + 1
        cnt[3] = cnt[3] + 1
    if count - jump == n:
        break
for i in cnt:
    print(i)

运行结果:

 

 

 

201909-1 小明种苹果

N, M = map(int, input().split())
matrix = [[i for i in map(int, input().split())] for j in range(N)]
Cut = []
total = []
for i in range(N):
    cut = 0
    for j in range(1, M + 1):
        cut = cut + matrix[i][j]
    Cut.append(cut)
    total.append(matrix[i][0] + cut)
for i in range(len(Cut)):
    Cut[i] = -Cut[i]
Max = max(Cut)
for i in range(len(Cut)):
    if Cut[i] == Max:
        oi = i
        break
print(sum(total), oi + 1, Max)

运行结果:

 

 

 

201903-1 小中大

n = int(input())
dig = input().split()
for i in range(n):
    dig[i] = int(dig[i])

lst = []
Max = max(dig)
Min = min(dig)
lst.append(Max)
lst.append(Min)
dig.sort()
if len(dig) % 2 != 0:
    med = dig[(0 + len(dig) - 1) // 2]
else:
    k = (0 + len(dig) - 1) // 2
    if (dig[k] + dig[k + 1]) % 2 == 0:
        med = (dig[k] + dig[k + 1]) // 2
    else:
        med = round((dig[k] + dig[k + 1]) / 2, 1)
lst.append(med)
lst.sort(reverse=True)
for i in lst:
    print(i, end=' ')

运行结果:

 

 

 

201812-1 小明上学

r, y, g = map(int, input().split())
n = int(input())
matrix = [[i for i in map(int, input().split())] for j in range(n)]
t = 0
for i in range(n):
    if matrix[i][0] == 0:
        t = t + matrix[i][1]
    elif matrix[i][0] == 1:
        t = t + matrix[i][1]
    elif matrix[i][0] == 2:
        t = t + matrix[i][1] + r
    else:
        t = t + 0
print(t)

运行结果:

 

 

201809-1 卖菜

n = int(input())
dig = input().split()
for i in range(n):
    dig[i] = int(dig[i])

Dig = []
Dig.append((dig[0] + dig[1]) // 2)
for i in range(1, n - 1):
    Dig.append((dig[i - 1] + dig[i] + dig[i + 1]) // 3)
Dig.append((dig[-2] + dig[-1]) // 2)
for i in Dig:
    print(i, end=' ')

运行结果:

 

 

 

201803-1 跳一跳

dig = input().split()
for i in range(len(dig)):
    dig[i] = int(dig[i])
s = dig.count(1)
total = []
cnt = 1
for i in range(len(dig)):
    if dig[i] == 2:
        total.append(i)
Total = []
if total:
    for i in range(1, len(total)):
        if total[i] - total[i - 1] == 1:
            cnt = cnt + 1
        else:
            Total.append(cnt)
            cnt = 1
    Total.append(cnt)
    for i in range(len(Total)):
        Total[i] = Total[i] * (Total[i] + 1)
print(s + sum(Total))

运行结果:

 

 

 

201712-1 最小差值

n = int(input())
dig = input().split()
for i in range(n):
    dig[i] = int(dig[i])
dig.sort()
div = []
for i in range(1, n):
    m = dig[i] - dig[i - 1]
    if m < 0:
        m = - m
    div.append(m)
print(min(div))

运行结果:

 

 

 

201709-1 打酱油

N = int(input())
res = N // 10
dig = []
div_1 = N // 50
N = N % 50
dig.append(div_1 * 2)
div_2 = N // 30
N = N % 30
dig.append(div_2 * 1)

res = res + sum(dig)
print(res)

运行结果:

 

 

 

201703-1 分蛋糕

n, k = map(int, input().split())
cake = input().split()
cnt = 0
for i in range(n):
    cake[i] = int(cake[i])
s = 0
sc = []
for i in range(n):
    s = s + cake[i]
    if s >= k:
        sc.append(i)
        cnt = cnt + 1
        s = 0

s = 0
if len(sc) != 0 and sc[-1] + 1 == n:
    cnt = cnt
else:
    cnt = cnt + 1
print(cnt)

运行结果:

 

 

 

201612-1 中间数

n = int(input())
dig = input().split()
for i in range(n):
    dig[i] = int(dig[i])

dig.sort()
Max = 0
Min = 0
flag = 0
for i in range(n):
    for j in range(n):
        if dig[j] < dig[i]:
            Min = Min + 1
        elif dig[j] > dig[i]:
            Max = Max + 1
    if Min == Max:
        flag = 1
        print(dig[i])
        break
    Min = 0
    Max = 0
if flag == 0:
    print(-1)

运行结果:

 

 

201609-1 最大波动

n = int(input())
dig = input().split()
for i in range(n):
    dig[i] = int(dig[i])

Max = []
for i in range(1, n):
    m = dig[i] - dig[i - 1]
    if m < 0:
        m = - m
    Max.append(m)
print(max(Max))

运行结果:

 

 

 

201604-1 折点计数

n = int(input())
dig = input().split()
for i in range(n):
    dig[i] = int(dig[i])
cnt = 0
if n == 1:
    print(0)
else:
    if dig[0] < dig[1]:
        flag = 1
    else:
        flag = 0
    for i in range(2, n):
        if flag == 1 and dig[i - 1] > dig[i]:
            cnt = cnt + 1
        elif flag == 0 and dig[i - 1] < dig[i]:
            cnt = cnt + 1
        if dig[i - 1] < dig[i]:
            flag = 1
        else:
            flag = 0
    print(cnt)
    

注意事项:

n == 1要单独考虑

运行结果:

 

 

 

201512-1 数位之和

n = int(input())
a = n // 10
b = n % 10
s = [b]
k = a
while a != 0:
    a = k // 10
    b = k % 10
    k = a
    s.append(b)
print(sum(s))

运行结果:

 

 

 

201509-1 数列分段

n = int(input())
dig = input().split()
for i in range(n):
    dig[i] = int(dig[i])

cnt = 1
for i in range(1, n):
    if dig[i - 1] != dig[i]:
        cnt = cnt + 1
print(cnt)

运行结果:

 

 

 

201503-1 图像旋转

m, n = map(int, input().split())
matrix = [[i for i in map(int, input().split())] for j in range(m)]
Mat = [[0 for i in range(m)] for j in range(n)]

t = n - 1
for i in range(n):
    for j in range(m):
        Mat[i][j] = matrix[j][t]
    t = t - 1
for i in range(n):
    for j in range(m):
        print(Mat[i][j], end=' ')
    print()

运行结果:

 

 

 

201412-1 门禁系统

n = int(input())
dig = input().split()
for i in range(n):
    dig[i] = int(dig[i])

cnt = [1 for i in range(n)]
for i in range(n):
    for j in range(i):
        if dig[i] == dig[j]:
            cnt[i] = cnt[i] + 1
for i in cnt:
    print(i, end=' ')

运行结果:

 

 

 

201409-1 相邻数对

n = int(input())
dig = input().split()
for i in range(n):
    dig[i] = int(dig[i])

dig.sort()
cnt = 0
for i in range(1, n):
    if dig[i] - dig[i - 1] == 1:
        cnt = cnt + 1
print(cnt)

运行结果:

 

 

 

201403-1 相反数

n = int(input())
dig = input().split()
for i in range(n):
    dig[i] = int(dig[i])

cnt = 0
for i in range(n):
    for j in range(n):
        if dig[j] + dig[i] == 0:
            cnt = cnt + 1
            dig[i] = dig[j] = 1001
print(cnt)

运行结果:

 

 

 

201312-1 出现次数最多的数

n = int(input())
dig = input().split()
for i in range(n):
    dig[i] = int(dig[i])

dig.sort()
# 创建集合
ss = list(set(dig))
cnt = []
for i in range(len(ss)):
    cnt.append(dig.count(ss[i]))

Max = max(cnt)
for i in range(len(ss)):
    if cnt[i] == Max:
        adress = i
        break
print(ss[adress])

运行结果:

 

 

文章出处登录后可见!

已经登录?立即刷新
退出移动版