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

目录

202305-2 矩阵运算

202303-2 垦田计划

202212-2 训练计划

202209-2 何以包邮?

202206-2 寻宝!大冒险!

202203-2 出行计划

202112-2 序列查询新解

202109-2 非零段划分

202104-2 邻域均值

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

202009-2 风险人群筛查

202006-2 稀疏向量

201912-2 回收站选址

201909-2 小明种苹果(续)

201903-2 二十四点

201812-2 小明放学

201809-2 买菜

201803-2 碰撞的小球

201712-2 游戏

201709-2 公共钥匙盒

201703-2 学生排队

201612-2 工资计算

201609-2 火车购票

201604-2 俄罗斯方块

201512-2 消除类游戏

201509-2 日期计算

201503-2 数字排序

201412-2 Z字形扫描

201409-2 画图

201403-2 窗口

201312-2 ISBN号码


202305-2 矩阵运算

202303-2 垦田计划

202212-2 训练计划

202209-2 何以包邮?

202206-2 寻宝!大冒险!

202203-2 出行计划

202112-2 序列查询新解

202109-2 非零段划分

202104-2 邻域均值

n, L, r, t = map(int, input().split())
matrix = [[i for i in map(int, input().split())] for j in range(n)]
location = []
for i in range(n):
    for j in range(n):
        x1 = i - r
        x2 = i + r
        y1 = j - r
        y2 = j + r
        if x1 < 0:
            x1 = 0
        if x2 >= n:
            x2 = n - 1
        if y1 < 0:
            y1 = 0
        if y2 >= n:
            y2 = n - 1
        location.append((x1, x2, y1, y2))
res = 0
print(location)
for i in range(len(location)):
    s = 0
    cnt = 0
    for j in range(location[i][0], location[i][1] + 1):
        for k in range(location[i][2], location[i][3] + 1):
            s = s + matrix[j][k]
            cnt = cnt + 1
    print(s)
    ave = s / cnt
    if ave <= t:
        res = res + 1
print(res)

70分暴力的 

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

只会暴力(只有70分)

202009-2 风险人群筛查

n, k, t, xl, yd, xr, yu = map(int, input().split())
matrix = [[i for i in map(int, input().split())] for j in range(n)]
io = [[0 for i in range(t)] for j in range(n)]

for i in range(n):
    for j in range(0, 2 * t, 2):
        if xl <= matrix[i][j] <= xr and yd <= matrix[i][j + 1] <= yu:
            io[i][j // 2] = 1
here = 0
stay = 0
for i in range(n):
    if 1 in io[i]:
        here = here + 1
for i in range(n):
    m = 0
    for j in range(t):
        if io[i][j] == 1:
            m = m + 1
            if m >= k:
                stay = stay + 1
                break
        else:
            m = 0

print(here)
print(stay)

运行结果:

 

 

 

202006-2 稀疏向量

n, a, b = map(int, input().split())
Dict = {}
res = 0
for i in range(a):
    index, num = map(int, input().split())
    Dict[index] = num
for j in range(b):
    index, num = map(int, input().split())
    if index in Dict:
        res = res + Dict[index] * num
print(res)
'''
10 3 4
4 5
7 -3
10 1
1 10
4 20
5 30
7 40
'''

运行结果:

 

 

 

201912-2 回收站选址

n = int(input())
matrix = [[i for i in map(int, input().split())] for j in range(n)]
add = []
for i in range(len(matrix)):
    cnt = 0
    for j in range(len(matrix)):
        if j == i:
            continue
        else:
            if matrix[i][0] == matrix[j][0] and matrix[i][1] == matrix[j][1] + 1:
                cnt = cnt + 1
            if matrix[i][0] == matrix[j][0] and matrix[i][1] == matrix[j][1] - 1:
                cnt = cnt + 1
            if matrix[i][0] == matrix[j][0] + 1 and matrix[i][1] == matrix[j][1]:
                cnt = cnt + 1
            if matrix[i][0] == matrix[j][0] - 1 and matrix[i][1] == matrix[j][1]:
                cnt = cnt + 1
    if cnt == 4:
        add.append(i)

Score = []
for i in range(len(matrix)):
    score = 0
    if i not in add:
        continue
    else:
        for j in range(len(matrix)):
            if j == i:
                continue
            else:
                if matrix[i][0] == matrix[j][0] + 1 and matrix[i][1] == matrix[j][1] + 1:
                    score = score + 1
                if matrix[i][0] == matrix[j][0] + 1 and matrix[i][1] == matrix[j][1] - 1:
                    score = score + 1
                if matrix[i][0] == matrix[j][0] - 1 and matrix[i][1] == matrix[j][1] + 1:
                    score = score + 1
                if matrix[i][0] == matrix[j][0] - 1 and matrix[i][1] == matrix[j][1] - 1:
                    score = score + 1
    Score.append(score)
res = [0, 0, 0, 0, 0]
for i in range(len(Score)):
    res[Score[i]] = res[Score[i]] + 1
for i in range(len(res)):
    print(res[i])
'''
7
1 2
2 1
0 0
1 1
1 0
2 0
0 1
'''
'''
2
0 0
-100000 10
'''
'''
11
9 10
10 10
11 10
12 10
13 10
11 9
11 8
12 9
10 9
10 11
12 11
'''

运行结果:

 

 

 

201909-2 小明种苹果(续)

n = int(input())
matrix = [[i for i in map(int, input().split())] for j in range(n)]
Flag = []
for i in range(n):
    for j in range(1, len(matrix[i])):
        if matrix[i][j] > 0:
            flag = j
    Flag.append(flag)
S = 0
for i in range(n):
    s = 0
    for j in range(Flag[i], matrix[i][0] + 1):
        s = s + matrix[i][j]
    S = S + s
D = []
for i in range(n):
    if Flag[i] == 1:
        continue
    compare = matrix[i][1]
    for j in range(2, len(matrix[i])):
        if matrix[i][j] > 0:
            if matrix[i][j] < compare:
                D.append(i + 1)
                break
        else:
            compare = compare + matrix[i][j]

E = 0
if len(D) < 3:
    E = 0
else:
    for i in range(len(D)):
        if i == len(D) - 2:
            if D[i] == D[-1] - 1 and D[-1] == D[0] + n- 1:
                E = E + 1
        elif i == len(D) - 1:
            if D[i] == D[0] + n - 1 and D[0] == D[1] - 1:
                E = E + 1
        else:
            if D[i] == D[i + 1] - 1 and D[i + 1] == D[i + 2] - 1:
                E = E + 1

print(S, len(D), E)
'''4
4 74 -7 -12 -5
5 73 -8 -6 59 -4
5 76 -5 -10 60 -2
5 80 -6 -15 59 0'''
'''
5
4 10 0 9 0
4 10 -2 7 0
2 10 0
4 10 -3 5 0
4 10 -1 8 0
'''

运行结果:

 

 

 

201903-2 二十四点

n = int(input())
matrix = [[i for i in list(input())] for j in range(n)]
dig = []
note = []
for i in range(len(matrix)):
    for j in range(len(matrix[i])):
        if '0' <= matrix[i][j] <= '9':
            matrix[i][j] = int(matrix[i][j])


def chengchu(lst):
    for i in range(len(lst)):
        cnt = 0
        if lst[i] == 0:
            cnt = cnt + 1
            continue
        if lst[i] == 'x' or lst[i] == '/':
            if lst[i] == 'x':
                lst[i - 1] = lst[i - 1 - cnt] * lst[i + 1]
            else:
                lst[i - 1] = lst[i - 1 - cnt] // lst[i + 1]
            lst[i] = 0
            lst[i + 1] = 0
            break
    for i in range(len(lst)):
        if lst[i] == 0:
            lst.pop(i)
            break
    for i in range(len(lst)):
        if lst[i] == 0:
            lst.pop(i)
            break
    return lst

def calculate(lst):
    for i in range(3):
        lst = chengchu(lst)

    if lst[-1] == '+' or lst[-1] == '-':
        lst.append(0)
    if lst[0] == '+' or lst[0] == '-':
        lst.append(0)
        for i in range(len(lst) - 1, 0, -1):
            lst[i] = lst[i - 1]
        lst[0] = 0
    for i in range(len(lst)):
        if type(lst[i]) == str and type(lst[i + 1]) == str:
            lst.pop(i)
            break
        
    res = lst[0]

    for i in range(len(lst)):
        if lst[i] == '+':
            res = res + lst[i + 1]
        elif lst[i] == '-':
            res = res - lst[i + 1]
    return res


for i in range(len(matrix)):
    if calculate(matrix[i]) == 24:
        print('Yes')
    else:
        print('No')

'''10
9+3+4x3
5+4x5x5
7-9-9+8
5x6/5x4
3+5+7+9
1x1+9-9
1x9-5/9
8/5+6x9
6x7-3x6
6x4+4/5'''

运行结果:

 

 

 

201812-2 小明放学

red, yellow, green = map(int, input().split())
n = int(input())
matrix = [[i for i in map(int, input().split())] for j in range(n)]
t = 0
a = []

for i in range(n):
    a.clear()
    T = t % (red + yellow + green)
    if matrix[i][0] == 0:
        t = t + matrix[i][1]
    elif matrix[i][0] == 1:
        a.append((1, matrix[i][1]))
        a.append((3, green + matrix[i][1]))
        a.append((2, green + yellow + matrix[i][1]))
        a.append((1, green + yellow + red + matrix[i][1]))
        if T < a[0][1]:
            t_ = a[0][1] - T
            t = t + t_
        elif a[0][1] <= T < a[1][1]:
            t = t + 0
        elif a[1][1] <= T < a[2][1]:
            t_ = a[2][1] - T + red
            t = t + t_
        elif a[2][1] <= T < a[3][1]:
            t_ = a[3][1] - T
            t = t + t_
    elif matrix[i][0] == 2:
        a.append((2, matrix[i][1]))
        a.append((1, red + matrix[i][1]))
        a.append((3, green + red + matrix[i][1]))
        a.append((2, green + yellow + red + matrix[i][1]))
        if T < a[0][1]:
            t_ = a[0][1] - T + red
            t = t + t_
        elif a[0][1] <= T < a[1][1]:
            t_ = a[1][1] - T
            t = t + t_
        elif a[1][1] <= T < a[2][1]:
            t = t + 0
        elif a[2][1] <= T < a[3][1]:
            t_ = a[3][1] - T + red
            t = t + t_
    else:
        a.append((3, matrix[i][1]))
        a.append((2, yellow + matrix[i][1]))
        a.append((1, yellow + red + matrix[i][1]))
        a.append((3, green + yellow + red + matrix[i][1]))
        if T < a[0][1]:
            t = t + 0
        elif a[0][1] <= T < a[1][1]:
            t_ = a[1][1] - T + red
            t = t + t_
        elif a[1][1] <= T < a[2][1]:
            t_ = a[2][1] - T
            t = t + t_
        elif a[2][1] <= T < a[3][1]:
            t = t + 0
print(t)

运行结果:

 

 

 

201809-2 买菜

n = int(input())
matrix1 = [[i for i in map(int, input().split())] for j in range(n)]
matrix2 = [[i for i in map(int, input().split())] for j in range(n)]
t = 0
for i in range(n):
    for j in range(n):
        if matrix2[j][1] < matrix1[i][0]:
            continue
        elif matrix1[i][1] < matrix2[j][0]:
            continue
        else:
            Min = max(matrix1[i][0], matrix2[j][0])
            Max = min(matrix1[i][1], matrix2[j][1])
            t = t + (Max - Min)
print(t)

运行结果:

 

 

 

201803-2 碰撞的小球

n, L, t = map(int, input().split())


def judge(num, end, lst):
    collide = []
    Collide = []
    for i in range(num):
        if lst[i] == end:
            collide.append(i)
        for j in range(i, num):
            if j == i:
                continue
            else:
                if lst[i] == lst[j] or lst[i] + lst[j] == 0:
                    Collide.append((i, j))
    return list(set(collide)), list(set(Collide))


Lst = input().split()
for i in range(n):
    Lst[i] = int(Lst[i])
for i in range(t):
    yP, YP = judge(n, L, Lst)
    for k in range(len(yP)):
        Lst[yP[k]] = - Lst[yP[k]]
    for r in range(len(YP)):
        Lst[YP[r][0]] = - Lst[YP[r][0]]
        Lst[YP[r][1]] = - Lst[YP[r][1]]
    for j in range(n):
        Lst[j] = Lst[j] + 1
for i in range(len(Lst)):
    if Lst[i] < 0:
        print(-Lst[i], end=' ')
    else:
        print(Lst[i], end=' ')

运行结果:

201712-2 游戏

n, k = map(int, input().split())
lst = [i + 1 for i in range(n)]
cnt = 1
if k == 1:
    print(lst[-1])
else:
    while n - lst.count(0) != 1:
        for i in range(len(lst)):
            if lst[i] == 0:
                continue
            else:
                if cnt % k == 0 or cnt % 10 == k:
                    lst[i] = 0
                cnt = cnt + 1

    for i in range(n):
        if lst[i] != 0:
            print(lst[i])
            break

运行结果:

201709-2 公共钥匙盒

N, K = map(int, input().split())
matrix = [[i for i in map(int, input().split())] for j in range(K)]
lst = [i + 1 for i in range(N)]


def fun_1(lst):
    return lst[1]


def fun_2(lst):
    return lst[2]


for i in range(K):
    matrix[i][2] = matrix[i][1] + matrix[i][2]
matrix.sort(key=fun_2, reverse=True)
Max = matrix[0][2] + 1
matrix.sort(key=fun_1)
Min = matrix[0][1]
Lst_1 = []  # 钥匙和取出列表
Lst_2 = []  # 钥匙和放回列表

for i in range(K):
    Lst_1.append([matrix[i][0], matrix[i][1]])
    Lst_2.append([matrix[i][0], matrix[i][2]])
a = []
oq = []
for i in range(Min, Max):
    for k in range(len(Lst_2)):
        if Lst_2[k][1] == i:
            a.append(Lst_2[k][0])
            a.sort()
    for op in range(len(lst)):
        if lst[op] == -1:
            oq.append(op)
    for oo in range(len(a)):
        lst[oq[oo]] = a[oo]
    oq.clear()
    a.clear()
    for j in range(len(Lst_1)):
        if Lst_1[j][1] == i:
            lst[lst.index(Lst_1[j][0])] = -1
for i in lst:
    print(i, end=' ')

运行结果:

201703-2 学生排队

n = int(input())
num = int(input())
lst = [i + 1 for i in range(n)]
matrix = [[i for i in map(int, input().split())] for j in range(num)]


def left(Number, step, Lst):
    for i in range(len(Lst)):
        if Lst[i] == Number:
            emo = i
            break
    Emo = emo - step
    for i in range(emo, Emo, -1):
        Lst[i] = Lst[i - 1]
    Lst[Emo] = Number
    return Lst


def right(Number, step, Lst):
    for i in range(len(Lst)):
        if Lst[i] == Number:
            emo = i
            break
    Emo = emo + step
    for i in range(emo, Emo):
        Lst[i] = Lst[i + 1]
    Lst[Emo] = Number
    return Lst


for i in range(num):
    if matrix[i][1] > 0:
        lst = right(matrix[i][0], matrix[i][1], lst)
    else:
        lst = left(matrix[i][0], -matrix[i][1], lst)
for i in lst:
    print(i, end=' ')

运行结果:

201612-2 工资计算

n = int(input())
wage = [0, 1500 * 0.03, 3000 * 0.1, 4500 * 0.2, (35000 - 9000) * 0.25, 20000 * 0.3, 25000 * 0.35]
Lst = [0]
s = 0
for i in range(1, len(wage)):
    s = s + wage[i]
    Lst.append(s)
for j in range(3500, 100001):
    s = 0
    if j % 100 != 0:
        continue
    else:
        i = j - 3500
        if i > 80000:
            s = Lst[6] + (i - 80000) * 0.45
            if s + n == j:
                print(j)
        elif i > 55000:
            s = Lst[5] + (i - 55000) * 0.35
            if s + n == j:
                print(j)
        elif i > 35000:
            s = Lst[4] + (i - 35000) * 0.3
            if s + n == j:
                print(j)
        elif i > 9000:
            s = Lst[3] + (i - 9000) * 0.25
            if s + n == j:
                print(j)
        elif i > 4500:
            s = Lst[2] + (i - 4500) * 0.2
            if s + n == j:
                print(j)
        elif i > 1500:
            s = Lst[1] + (i - 1500) * 0.1
            if s + n == j:
                print(j)
        else:
            s = i * 0.03
            if s + n == j:
                print(j)
if n < 3500:
    print(n)

运行结果:

 

201609-2 火车购票

matrix = [[0 for j in range(5)] for i in range(20)]
n = int(input())
Need = input().split()
for i in range(n):
    Need[i] = int(Need[i])

for i in range(n):
    flag = 0

    if Need[i] == 1:
        for j in range(20):
            if flag == 1:
                break
            for k in range(5):
                if matrix[j][k] == 0:
                    print(5 * j + k + 1)
                    matrix[j][k] = 1
                    flag = 1
                    break

    elif Need[i] == 2:
        for j in range(20):
            if flag == 1:
                break
            for k in range(4):
                if matrix[j][k] == 0 and matrix[j][k + 1] == 0:
                    print(5 * j + k + 1, 5 * j + k + 2)
                    matrix[j][k] = 1
                    matrix[j][k + 1] = 1
                    flag = 1
                    break

    elif Need[i] == 3:
        for j in range(20):
            if flag == 1:
                break
            for k in range(3):
                if matrix[j][k] == 0 and matrix[j][k + 1] == 0 and matrix[j][k + 2] == 0:
                    print(5 * j + k + 1, 5 * j + k + 2, 5 * j + k + 3)
                    matrix[j][k] = 1
                    matrix[j][k + 1] = 1
                    matrix[j][k + 2] = 1
                    flag = 1
                    break
    elif Need[i] == 4:
        for j in range(20):
            if flag == 1:
                break
            for k in range(2):
                if matrix[j][k] == 0 and matrix[j][k + 1] == 0 and matrix[j][k + 2] == 0 and matrix[j][k + 3] == 0:
                    print(5 * j + k + 1, 5 * j + k + 2, 5 * j + k + 3, 5 * j + k + 4)
                    matrix[j][k] = 1
                    matrix[j][k + 1] = 1
                    matrix[j][k + 2] = 1
                    matrix[j][k + 3] = 1
                    flag = 1
                    break
    else:
        k = 0
        for j in range(20):
            if flag == 1:
                break
            if matrix[j][k] == 0 and matrix[j][k + 1] == 0 and matrix[j][k + 2] == 0 and matrix[j][k + 3] == 0 and matrix[j][k + 4] == 0:
                print(5 * j + k + 1, 5 * j + k + 2, 5 * j + k + 3, 5 * j + k + 4, 5 * j + k + 5)
                matrix[j][k] = 1
                matrix[j][k + 1] = 1
                matrix[j][k + 2] = 1
                matrix[j][k + 3] = 1
                matrix[j][k + 4] = 1
                flag = 1
                break

运行结果:

201604-2 俄罗斯方块

matrix_1 = [[i for i in map(int, input().split())] for j in range(15)]
matrix_2 = [[i for i in map(int, input().split())] for j in range(4)]
n = int(input())
Lst = []  # matrix_2矩阵全0的行下标
for i in range(3, -1, -1):
    cnt = 0
    Lst.append([])
    for j in range(4):
        if matrix_2[i][j] == 0:
            cnt = cnt + 1
    if cnt != 4:
        for k in range(4):
            if matrix_2[i][k] == 1:
                Lst[3 - i].append(k)
for i in range(4):
    if len(Lst[i]) == 0:
        continue
    else:
        emo = 14
        if len(Lst[i]) == 1:
            emo = 14
            for j in range(15):
                if matrix_1[j][n + Lst[i][0] - 1] == 1:
                    emo = j - 1
                    break
            matrix_1[emo][n + Lst[i][0] - 1] = 1
        elif len(Lst[i]) == 2:
            emo = 14
            for j in range(15):
                if matrix_1[j][n + Lst[i][0] - 1] == 1 or matrix_1[j][n + Lst[i][1] - 1] == 1:
                    emo = j - 1
                    break
            matrix_1[emo][n + Lst[i][0] - 1] = matrix_2[3 - i][Lst[i][0]]
            matrix_1[emo][n + Lst[i][1] - 1] = matrix_2[3 - i][Lst[i][1]]
        elif len(Lst[i]) == 3:
            emo = 14
            for j in range(15):
                if matrix_1[j][n + Lst[i][0] - 1] == 1 or matrix_1[j][n + Lst[i][1] - 1] == 1 or matrix_1[j][n + Lst[i][2] - 1] == 1:
                    emo = j - 1
                    break
            matrix_1[emo][n + Lst[i][0] - 1] = matrix_2[3 - i][Lst[i][0]]
            matrix_1[emo][n + Lst[i][1] - 1] = matrix_2[3 - i][Lst[i][1]]
            matrix_1[emo][n + Lst[i][2] - 1] = matrix_2[3 - i][Lst[i][2]]
        else:
            emo = 14
            for j in range(15):
                if matrix_1[j][n + Lst[i][0] - 1] == 1 or matrix_1[j][n + Lst[i][1] - 1] == 1 or matrix_1[j][n + Lst[i][2] - 1] == 1 or matrix_1[j][n + Lst[i][3] - 1] == 1:
                    emo = j - 1
                    break
            matrix_1[emo][n + Lst[i][0] - 1] = matrix_2[3 - i][Lst[i][0]]
            matrix_1[emo][n + Lst[i][1] - 1] = matrix_2[3 - i][Lst[i][1]]
            matrix_1[emo][n + Lst[i][2] - 1] = matrix_2[3 - i][Lst[i][2]]
            matrix_1[emo][n + Lst[i][3] - 1] = matrix_2[3 - i][Lst[i][3]]


for i in range(15):
    for j in range(10):
        print(matrix_1[i][j], end=' ')
    print()

运行结果:

201512-2 消除类游戏

n, m = map(int, input().split())
matrix = [[i for i in map(int, input().split())] for j in range(n)]
Lst = []
cnt = 1
flag = 0
for i in range(n):
    for j in range(m):
        flag = 0
        for p in range(i + 1, n):
            if matrix[i][j] != matrix[p][j]:
                flag = 1
                break
            else:
                cnt = cnt + 1
        if cnt >= 3:
            if flag == 1:
                k = p
            else:
                k = p + 1
            for r in range(i, k):
                Lst.append((r, j))
        flag = 0
        cnt = 1
        for q in range(j + 1, m):
            if matrix[i][j] != matrix[i][q]:
                flag = 1
                break
            else:
                cnt = cnt + 1
        if cnt >= 3:
            if flag == 1:
                k = q
            else:
                k = q + 1
            for s in range(j, k):
                Lst.append((i, s))
        flag = 0
        cnt = 1
b = list(set(Lst))
for i in range(len(b)):
    matrix[b[i][0]][b[i][1]] = 0
for i in range(n):
    for j in range(m):
        print(matrix[i][j], end=' ')
    print()

运行结果:

201509-2 日期计算

year = int(input())
day = int(input())
L = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
    flag = 1
    L[2] = 29
else:
    flag = 0

s = 0
for i in range(len(L)):
    s = s + L[i]
    if day <= s:
        print(i)
        break

print(day - (s - L[i]))

运行结果:

201503-2 数字排序

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

L = list(set(cnt))
L.sort()


def fun(li):
    return li[1]


L.sort(key=fun, reverse=True)
for i in range(len(L)):
    print(L[i][0], L[i][1])

运行结果:

201412-2 Z字形扫描

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


def left_down(a, b, N):
    lst_1 = []
    while a < N - 1 and b > 0:
        a = a + 1
        b = b - 1
        lst_1.append((a, b))
    return lst_1


def right_up(a, b, N):
    lst_2 = []
    while a > 0 and b < N - 1:
        a = a - 1
        b = b + 1
        lst_2.append((a, b))
    return lst_2


lst = []
i = 0
j = 1

if n == 1:
    print(matrix[0][0])
elif n == 2:
    print(matrix[0][0], matrix[0][1], matrix[1][0], matrix[1][1])
else:
    print(matrix[0][0], matrix[0][1], end=' ')
    flag = 1  # flag == 1 进行斜操作 flag == 2 进行平移操作

    while (i, j) != (n - 1, n - 1):
        if flag == 1:
            if i == 0 or j == n - 1:
                L = left_down(i, j, n)
                for i in range(len(L)):
                    print(matrix[L[i][0]][L[i][1]], end=' ')
                i = L[-1][0]
                j = L[-1][1]
                flag = 2
            else:
                L = right_up(i, j, n)
                for i in range(len(L)):
                    print(matrix[L[i][0]][L[i][1]], end=' ')
                i = L[-1][0]
                j = L[-1][1]
                flag = 2
        else:
            if (j == 0 or j == n - 1) and i != n - 1:
                i = i + 1
                j = j
                print(matrix[i][j], end=' ')
                flag = 1
            else:
                i = i
                j = j + 1
                print(matrix[i][j], end=' ')
                flag = 1


运行结果:

201409-2 画图

matrix = [[0 for i in range(0, 110)] for j in range(0, 110)]
print(matrix[0][0])
n = int(input())
Mat = [[i for i in map(int, input().split())] for j in range(n)]
for i in range(n):
    a = Mat[i][0]
    b = Mat[i][1]
    c = Mat[i][2]
    d = Mat[i][3]
    for j in range(a + 1, c + 1):
        for k in range(b + 1, d + 1):
            matrix[j][k] = 1
s = 0
for i in range(1, 101):
    for j in range(1, 101):
        if matrix[i][j] == 1:
            s = s + 1
print(s)

运行结果:

201403-2 窗口

N, M = map(int, input().split())
matrix = [[i for i in map(int, input().split())] for j in range(N)]  # 窗口位置
Matrix = [[i for i in map(int, input().split())] for j in range(M)]  # 鼠标位置
Flag = N - 1  # 记录最优先窗口

for i in range(M):
    flag = 0
    if matrix[Flag][0] <= Matrix[i][0] <= matrix[Flag][2] and matrix[Flag][1] <= Matrix[i][1] <= matrix[Flag][3]:
        flag = 1
        print(Flag + 1)
    else:
        for j in range(N - 1, -1, -1):
            if j == Flag:
                continue
            else:
                if matrix[j][0] <= Matrix[i][0] <= matrix[j][2] and matrix[j][1] <= Matrix[i][1] <= matrix[j][3]:
                    flag = 1
                    print(j + 1)
                    Flag = j
                    break
    if flag == 0:
        print('IGNORED')

运行结果:(只有90,具体问题不知,有大佬知道可以写在评论区或私信)

 

201312-2 ISBN号码

String = input().split('-')
dig_1 = []
dig_2 = []
for i in range(3):
    String[i] = int(String[i])
if String[3] != 'X':
    String[3] = int(String[3])
a = String[1] // 10
b = String[1] % 10
dig_1.append(b)
k = a
while a != 0:
    a = k // 10
    b = k % 10
    k = a
    dig_1.append(b)
a = String[2] // 10
b = String[2] % 10
dig_2.append(b)
k = a
while a != 0:
    a = k // 10
    b = k % 10
    k = a
    dig_2.append(b)

dig_1.reverse()
dig_2.reverse()
s = String[0] * 1
cnt = 2
for i in range(3):
    s = s + dig_1[i] * cnt
    cnt = cnt + 1
for i in range(5):
    s = s + dig_2[i] * cnt
    cnt = cnt + 1
res = s % 11
if res == 10:
    res = 'X'


if res == String[3]:
    print('Right')
else:
    print(String[0], end='')
    print('-', end='')
    for i in range(3):
        print(dig_1[i], end='')
    print('-', end='')
    for i in range(5):
        print(dig_2[i], end='')
    print('-', end='')
    print(res)

运行结果:

你可能感兴趣的:(算法,python)