CCF-CSP 试题考前练习(Python实时更新~)注释超详细

现值计算(202212-1)

  • 解题思路:根据题目中所给公式便可得出结果
  • 注意事项:注意最后的输出结果的格式
t = list(input().strip().s plit())
data = list(map(int, input().split()))
n, i = int(t[0]), float(t[1])
res = 0
for x in range(len(data)):
    res += data[x] * (1 + i) ** (-x)

print("{:.3f}".format(res))

  • 拓展了解
## format 用法
## 格式化字符串
## 使用str.format()

## 可以接受不限个参数,位置可以不按顺序
"{} {}".format("hello", "world") # 不设置指定位置,按默认顺序 
"{0} {1}".format("hello", "world")  # 设置指定位置
"{1} {0} {1}".format("hello", "world")  # 设置指定位置

##  也可以设置参数
print("名字:{name}, 性别{sex}".format(name = "小红", sex = "女"))

## 数字格式化
## eg:
{:.2f}.format(3.1415926}  保留小数点后两位 ## 输出3.14  
{:+.2f}  带符号保留小数点后两位
{:-.2f}  带符号保留小数点后两位
{:.0f}   不带小数
{:0>2d}  数字补零 (填充左边, 宽度为2)
{:x<4d}  数字补x (填充右边, 宽度为4)
{:,}     以逗号分隔的数字格式
{:.2%}   百分比格式
{:.2e}   指数记法
{:>10d}  右对齐 (默认, 宽度为10)
{:<10d}  左对齐 (宽度为10)
{:^10d}  中间对齐 (宽度为10)
^, <, > 分别是居中、左对齐、右对齐,后面带宽度, : 号后面带填充的字符,只能是一个字符,不指定则默认是用空格填充。
+ 表示在正数前显示 +,负数前显示 -;  (空格)表示在正数前加空格
b、d、o、x 分别是二进制、十进制、八进制、十六进制。

训练计划(202212-2)

  • 解题思路
n, m = map(int, input().split())
early = [0] * (m + 1) # 记录最早完成时间
late = [0] * (m + 1)  # 记录最晚完成时间
flag = 1
p = [0] + list(map(int, input().split())) # 输入是否有依赖的科目,下标从1开始,第0位补0
t = [0] + list(map(int, input().split())) # 输入训练某科目需要花费的时间

for i in range(1, m + 1):
    if  p[i]  == 0:
    # 判断受有依赖的科目,如果为0,说明没有依赖的科目,直接从第1天开始
        early[i] = 1
    else:
    # 如果有依赖的科目,则该科目最早开始的时间为被依赖科目的最早开始时间+训练需要花费时间
        early[i] = early[p[i]]  + t[p[i]]
    # 如果最早开始时间训练完成后大于n,则没有最晚开始时间
    if  early[i] + t[i] - 1 >  n:
        flag = 0
print(' '.join(map(str, early[1 : m + 1])))
if flag == 1:
    for i in range(m, 0, -1):
        temp =366
        # 寻找第i科目是否有被依赖的科目
        for j in range(i + 1, m + 1):
            if p[j] == i:
                temp = min(temp, late[j])
        # 如果没有被依赖,则等于n-消耗时间+1
        if temp == 366:
            late[i] = n - t[i] + 1
        else:
            late[i] = temp - t[i]
    print('  '.join(map(str, late[1 :  m + 1])))

JPEG解码(202212-3)

  • 这道题自己在本地可以过,但是提交到平台上就运行错误,不太清楚是哪里错了
import math

Q = [[0 for j in range(8)] for i in range(8)]
M = [[0 for j in range(8)] for i in range(8)] # 扫描矩阵初始化
M_1 = [[0 for j in range(8)] for i in range(8)] # 矩阵M与量化矩阵相乘后的结果
M_2 = [[0 for j in range(8)] for i in range(8)] # 经过离散变换后的矩阵
M_3 = [[0 for j in range(8)] for i in range(8)] # 经过加128取整后的矩阵
# 离散余弦逆变换函数
def discrete_cos_fun(x, y):
    m = 0
    for u in range(8):
        temp = 0
        for v in range(8):
            if u == 0:
                if v == 0:
                    temp += u1 * v1 * M_1[u][v] * math.cos((math.pi / 8) * (x + 0.5) * u) * math.cos((math.pi / 8) * (y + 0.5) * v) * 0.25
                else:
                    temp += u1 * v2 * M_1[u][v] * math.cos((math.pi / 8) * (x + 0.5) * u) * math.cos((math.pi / 8) * (y + 0.5) * v) * 0.25
            else:
                if v == 0:
                    temp += u2 * v1 * M_1[u][v] * math.cos((math.pi / 8) * (x + 0.5) * u) * math.cos((math.pi / 8) * (y + 0.5) * v) * 0.25
                else:
                    temp += u2 * v2 * M_1[u][v] * math.cos((math.pi / 8) * (x + 0.5) * u) * math.cos((math.pi / 8) * (y + 0.5) * v) * 0.25
        m += temp

    m = round(m, 2)

    return m

# 取整函数:
def turn(x):
    x = round(x + 128)
    if x < 0:
        x = 0
    if x > 255:
        x = 255
    return x

# 输出函数
def print_result(P):
    for x in range(8):
        for y in range(8):
            print(P[x][y], end = ' ')
        print()
    

# 输入量化矩阵
for x in range(8):
    Q[x] = list(map(int, input().split()))
# 输入扫描数据的个数
n = int(input())

# 数字T = 0, 1, 2
# 0:输出填充后的图像矩阵
# 1:输出量化后的图像矩阵
# 2:输出最终的解码结果
T = int(input())

# 输入扫描数据
t = list(map(int, input().split()))
#print(t)
 
i, j = 0, 0 # 扫描矩阵每次扫描数据的位置
direction  = 2 # 扫描矩阵每次的方向,1为左下,2为右上

for x in range(n):
   
    # 将数据赋值给扫描矩阵
    if i >= 0 and i < n and j >= 0 and j < n:
        M[i][j] = t[x]
    #移动扫描矩阵位置:
    if direction == 1:
        i += 1
        j -= 1
    if direction == 2:
        i -= 1
        j += 1
    # 规则1:
    if i < 0:
        i = 0
        direction = 1
    # 规则2:
    if j < 0:
        j = 0
        direction = 2
# 将矩阵M与量化矩阵相乘
for x in range(8):
    for y in range(8):
        M_1[x][y] = M[x][y] * Q[x][y]
# 对矩阵进行离散余弦逆变换
u1, u2 = math.pow(0.5,0.5), 1
v1, v2 = math.pow(0.5,0.5), 1

for x in range(8):
    for y in range(8):
        M_2[x][y] = discrete_cos_fun(x, y)

# 将矩阵的每个元素都加上128
for x in range(8):
    for y in range(8):
        M_3[x][y] = turn(M_2[x][y])

#print_result(M_2)
if T == 0:
    print_result(M)
if T == 1:
    print_result(M_1)
if T == 2:
    print_result(M_3)
                   

如此编码(202209-1)

n, m = map(int, input().split())
a = [0] + list(map(int, input().split()))
c = [0] * (n + 1) # 前缀乘积数组
c[0] = 1
for i in range(1, n + 1):
    c[i] = c[i - 1] * a[i]

b = [0] * (n + 1) # 正确答案的数组
temp= [0] * (n + 1) # 存放m % ci的结果
for i in range(1, n + 1):
    temp[i] = m % c[i]

for i in range(1, n + 1):
    b[i] = (temp[i] - temp[i - 1]) // c[i - 1]

for i in range(1, n +1):
    print(b[i], end = ' ')

何以包邮(202209-2)

n, x = map(int,input().split()) # 图书数量和包邮条件
f = [0] * (n * x) # 存储花费
a = [0] * ( n + 1) # 存储每个数的价格 
# 进行遍历,将每本书的价格依次存入a
for i in range(1, n + 1):
    a[i] = int(input())
    
pre = sum(a) #满足m >= x的最小花费,初始值设置为总和
for i in range(1, n + 1):
    for j in range(pre,a[i] - 1, -1):
        f[j] = max(f[j], f[j - a[i]] + a[i])
for i in range(x, pre + 1):
    if f[i] >= x:
        print(f[i])
        break

归一化处理(202206-1)

import math
def turn_D(a,average):
    res = 0
    for i in range(len(a)):
        res += math.pow(a[i] - average, 2)
    res /= len(a)
    return res
def turn_f(a,average,d):
    res = 0
    d = math.sqrt(d)
    res = (a - average) / d
    return res
n = int(input())
a = list(map(int, input().split()))
average = sum(a) / n
d = turn_D(a, average)
for i in range(n):
    res = turn_f(a[i], average, d)
    print(res)

寻宝!大冒险!(202206-2)

# 树的棵数、绿化图和藏宝图的大小
n, l, s = map(int, input().split())
# 以集合的形式存储树的坐标
points = {}
for i in range(n):
    x, y = map(int, input().split())
    points.update({(x,y):1})
# 建立藏宝图
value = []
for i in range(s+1):
    value.insert(0, list(map(int, input().split())))
# res值来记录绿化图中有多少处坐标可以与藏宝图左下角对应
res = 0
# 开始遍历树的坐标与藏宝图中的树的坐标进行比对
for x, y in points:
    # 设置一个标志值来判定是否符合藏宝图要求
    flag = 0
    # 开始遍历比对
    for i in range(s + 1):
        for j in range(s + 1):
            if (x + i > l) or (y + j > l): 
                flag = 1
                break
            if value[i][j]: 
                if (x + i, y + j) not in points:
                    flag = 1
                    break
            else:      
                if (x + i, y + j) in points:
                    flag = 1
                    break
        if flag == 1:  
            break
    if flag == 0:
        res += 1
print(res)


未初始化警告(202203-1)

  • 版本1
N = int(1e5) + 10
flag = [False] * N
n, k = map(int, input().split())
res = 0
for i in range(k):
    x, y = map(int, input().split())
    if not flag[y] and y != 0 or not flag[x] and x == y:
        res += 1
    
    flag[x] = True
    
    
print(res)
  • 版本2(这个纯暴力,然后超时了,只得一半分)
##l, r = [], []
##n, k = map(int, input().split())
##for i in range(k):
##    x, y = map(int, input().split())
##    l.append(x)
##    r.append(y)
##res = 0
##for i in range(k):
##    if r[i] not in l[:i] and r[i] != 0:
##        res += 1
##
##print(res)

出行计划(202203-2)

  • 70分题解
def query(q,plan):
    cnt = 0
    for i in plan:
        if i[0] < q + i[1] + k and i[0] >= q + k:
            cnt += 1
    return cnt

# 出行计划数目、查询个数、等待核酸检测结果所需时间
n, m, k = map(int, input().split())
# 每项出行计划
plan = set()
for i in range(n):
    tmp = tuple(map(int,input().split()))
    plan.add(tmp)
for i in range(m):
    q = int(input())
    print(query(q,plan))
  • 满分题解
N = int(2e5) + 10
diff = [0] * N
# 出行计划数目、查询个数、等待核酸检测结果所需时间
n, m, k = map(int, input().split())

for i in range(n):
    t, c = map(int, input().split())
    # 在[l, r] 时间段内做核酸,则t时刻可进入
    l = max(t - k - c + 1, 0)
    r = max(t - k, 0)
    # 在[l, r]时间段内能出行的计划的个数+1
    diff[l] += 1
    diff[r + 1] -= 1
# 利用差分计算每个时间能出行个数
for i in range(1, N):
    diff[i] += diff[i - 1]
for j in range(m):
    q = int(input())
    res = diff[q]
    print(res)


序列查询(202112-1)

#两个正整数n, N,分别代表n件商品,N代表最大预算
n, N = map(int, input().split())
#输入n 个用空格分隔的整数 A1,A2,⋯,An
a = list(map(int, input().split()))
a.insert(0, 0)
res = 0
for i in range(2, len(a)):
    res += (a[i]- a[i - 1]) * (i - 1)
if N > a[n - 1]:
    res += (N - a[n]) * n
print(res)


序列查询新解(202112-2)

n, N = map(int, input().split())
a = [0] + list(map(int, input().split()))
r = N // (n + 1)
g, f = [0] * N, [0] * N
for i in range(1, n):
    # 对f(x)进行赋值
    f[a[i]: a[i - 1]] = [i] * (a[i + 1] - a[i])
if N > a[n - 1]:
    f[a[n]: N] = [n] * (N - a[n])
# 计算g(x)
for i in range(N):
    g[i] = i // r
error = 0
for i in range(N):
    error += abs(g[i] - f[i])
print(error)

数组推导(202109-1)

n = int(input())
b = list(map(int, input().split()))
max_res = sum(b)
s = set(b)
min_res = sum(s)
print(max_res)
print(min_res)
    

非零段划分(202209-2)

# 差分数组加前缀和
M = int(1e4) + 5
b = [0] * M
n = int(input())
a = list(map(int, input().split()))
a.insert(0, 0)
for i in range(1, n + 1):
    # a[i - 1]到 a[i] - 1段的p都能构成新的非零段
    if a[i] > a[i - 1]:
        # 处理差分数组以实现区间整体+1
        b[a[i]] -= 1
        b[a[i - 1]] += 1
ans = 0
for i in range(1, M):
    b[i] += b[i - 1]
    ans = max(b[i], ans)
print(ans)
    

    

灰度直方图(202104-1)

n, m, l = map(int, input().split())
a = []
h = [0] * l
for i in range(n):
    a.extend(list(map(int, input().split())))
for i in range(l):
    h[i] = a.count(i)
print(' '.join(map(str, h)))

邻域均值(202204-2)

# 像素个数,灰度值范围, 邻域范围, 阈值
n, l, r, t = map(int, input().split())
# 输入矩阵
a = [0]
# 前缀和矩阵
b = [[0 for j in range(n + 1)] for i in range(n + 1)]
for i in range(n):
    tmp = list(map(int, input().split()))
    tmp.insert(0, 0)
    a.append(tmp)

# 预处理前缀和
for i in range(1, n + 1):
    for j in range(1, n + 1):
        b[i][j] = b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1] + a[i][j]

res = 0
for i in range(1, n + 1):
    for j in range(1, n + 1):
        # 判断矩阵的边界:
        x1 = max(i - r, 1)
        y1 = max(j - r, 1)
        x2 = min(i + r, n)
        y2 = min(j + r, n)
        # 计算矩阵中点的个数
        tt = (x2 - x1 + 1) * (y2 - y1 + 1)
        if b[x2][y2] - b[x2][y1 - 1] - b[x1 - 1][y2] + b[x1 - 1][y1 - 1] <= tt * t:
            res += 1
print(res)

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

N = int(1e5) + 10
score, w = [0] * N, [0] * N
n = int(input())
for i in range(n):
    score[i], w[i] = map(int, input().split())
y = 0
for i in range(n):
    y += score[i] * w[i]

y = max(0, y)
print(y)

期末预测之最佳阈值(202012-2)

  • 60分题解
N = int(1e5) + 10
y , result ,pre = [0] * N, [0] * N, [0] * N
m = int(input())
for i in range(m):
    y[i], result[i] = map(int, input().split())
tmp = set(y[:m])
res , data = 0, 0
def predict(x, y):
    global pre
    for i in range(m):
        if y[i] < x:
            pre[i] = 0
        else:
            pre[i] = 1
            
def sum_pre(pre, result):
    val = 0
    for i in range(m):
        if pre[i] == result[i]:
            val += 1
    return val
for i in tmp:
    predict(i, y)
    if sum_pre(pre, result) >= res:
        res = sum_pre(pre, result)
        data = i
    pre = [0] * (m + 10)
print(data)
    

称检测点查询(202009-1)

points = [0]
distance = {}
n, X, Y = map(int, input().split())
for i in range(n):
    points.append(tuple(map(int, input().split())))
d = 0
for i in range(1, n + 1):
    d = (X - points[i][0]) ** 2 + (Y - points[i][1]) ** 2
    distance[i] = d

a = sorted(distance.items(), key = lambda x: x[1])
for i in range(3):
    print(a[i][0])

风险人群筛查(202009-2)

points = []
n, k, t, xl, yd, xr, yu = map(int,input().split())
res =[0] * n # 记录每个人是 未停留0 or 1次 or k次

for i in range(n):
    points.append(list(map(int, input().split())))
    cnt = 0 # 分有连续多少个坐标位于高风险区域内,
    for j in range(0, 2 * t, 2):
        if xl <= points[i][j] <= xr and yd <= points[i][j + 1] <= yu:
            cnt += 1
            res[i] = 1
            if cnt >= k:
                res[i] = k
                break
        else:
            cnt = 0
          
print(n - res.count(0))
print(res.count(k))
    

线性分类器(202006-1)

points = [] # n个点的信息
A_group , B_group= [], [] # 如果值< 0 ,存入A,否则存入B
q = [] # m个查询
n, m = map(int, input().split())
for i in range(n):
    points.append(list(input().split()))
for i in range(m):
    q.append(list(map(int, input().split())))
for c, a, b in q:
    A_group.clear()
    B_group.clear()
    for x in points:
        if a * int(x[0]) + b * int(x[1]) + c < 0:
            A_group.append(x[2])
        else:
            B_group.append(x[2])
    if  'A' in  A_group and 'B' in A_group or 'A' in B_group and  'B' in B_group:
        print('No')
        continue
    else:
        print('Yes')


稀疏向量(202006-2)

u  =  {}
v  = {}
n, a, b = map(int,input().split())
for i in range(a):
    x, y = map(int, input().split())
    u.update({x : y})
for i in range(b):
    x, y = map(int, input().split())
    v.update({x : y})
if a >= b:
    res = 0
    for i in u.keys():
        if i in v:
            res += u[i] * v[i]
        else:
            continue
else:
    res = 0
    for i in v.keys():
        if i in u:
            res += u[i] * v[i]
print(res)

报数(201912-1)

n = int(input())
cnt = [0] * 5
i = 0
flag = 0
while 1:
    i += 1
    if i % 4 == 1 and (i % 7 == 0 or '7' in str(i)):
        cnt[1] += 1
    elif i % 4 == 2 and (i % 7 == 0 or '7' in str(i)):
        cnt[2] += 1
    elif i % 4 == 3 and (i % 7 == 0 or '7' in str(i)):
        cnt[3] += 1
    elif i % 4 == 0 and (i % 7 == 0 or '7' in str(i)):
        cnt[4] += 1
    else:
        flag += 1
    if flag == n:
        break
for i in range(1, 5):
    print(cnt[i])

回收站选址(201902-2)

dx1 = [0, 0, 1, -1]
dy1 = [1, -1, 0, 0]
dx2 = [-1, 1, 1, -1]
dy2 = [1, 1, -1, -1]
points = {}
garbage = {}
res = [0] * 5
n = int(input())
for i in range(n):
    points.update({tuple(map(int, input().split())) : 1})
for i in points:
    flag = 1
    for j in range(4):
        tmp1, tmp2 = i[0] - dx1[j], i[1] - dy1[j]
        if (tmp1, tmp2) not in points:
            flag = 0
            break
    if flag == 1:
        garbage.update({i : 0})
for i in garbage.keys():
    for j in range(4):
        tmp1, tmp2 = i[0] - dx2[j], i[1] - dy2[j]
        if  (tmp1, tmp2) in points:
            garbage[i] += 1
for j in garbage.values():
    res[j] += 1
for i in range(5):
    print(res[i])
  

小明种苹果(201909-1)

n, m = map(int, input().split()) # 苹果树的棵树和疏果操作的轮数
cnt = [0] * (n + 5) # 存储每棵树的疏果个数,其中cnt[0]代表未疏果之前苹果树上有苹果的个数
k , p = 0, 0 # 存储最大疏果的下标,以及最疏果的数量
for i in range(1, n + 1):
    tmp = list(map(int, input().split()))
    cnt[0] += tmp[0]
    cnt[i] = sum(tmp[1 : m+ 1])
t = cnt[0] -  abs(sum(cnt[1: ]))# 最后一轮疏果操作后所有苹果树上上下的苹果树总数
x = min(cnt[1 : n + 1]) # 最大疏果个数
k = cnt.index(x)
p = abs(cnt[k])
print(t, ' ', k, ' ', p)



评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值