洛谷刷题记录(python)【入门4】数组

【入门4】数组icon-default.png?t=M0H8https://www.luogu.com.cn/training/103#problemsP1428 小鱼比可爱

n = int(input())
a = [int(i) for i in input().split()]
b = []
for i in range(n):
    b.append(0)
for i in range(1, n):
    for j in range(i):
        if(a[i] > a[j]):
            b[i] += 1
for i in range(n-1):
    print(b[i], end=' ')
print(b[n-1])

P1427 小鱼的数字游戏

a = [int(i) for i in input().split()]
l = len(a)
b = a[0:l-1]
b.reverse()
for i in range(l-2):
    print(b[i], end=' ')
print(b[l-2])

P5727 【深基5.例3】冰雹猜想

n = int(input())
a = []
while 1:
    if n == 1:
        break
    a.append(int(n))
    if n % 2 == 0:
        n = int(n / 2)
    else:
        n = int(n * 3 + 1)

a.append(1)
a.reverse()
l = len(a)
for i in range(l-1):
    print(a[i], end=' ')
print(a[l-1])

P1047 [NOIP2005 普及组] 校门外的树

n, l = map(int, input().split())
a = []
for i in range(n):
    a.append(1)
a.append(1)
while l > 0:
    x, y = map(int, input().split())
    for i in range(x, y+1):
        a[i] = 0
    l -= 1
cnt = 0
for i in range(n+1):
    if a[i] == 1:
        cnt += 1
print(cnt)

P5728 【深基5.例5】旗鼓相当的对手

import math

class Stu:
    def __init__(self, x, y, z, sum):
        self.x = x
        self.y = y
        self.z = z
        self.sum = sum

n = int(input())
a = []
t = n
while t > 0:
    x, y, z = map(int, input().split())
    sum = x + y + z
    a.append(Stu(x, y, z, sum))
    t -= 1

ans = 0
for i in range(n-1):
    for j in range(i+1, n):
        if math.fabs(a[i].x - a[j].x) <= 5 and math.fabs(a[i].y - a[j].y) <= 5 and math.fabs(a[i].z - a[j].z) <= 5 and math.fabs(a[i].sum - a[j].sum) <= 10:
            ans += 1

print(ans)

P5729 【深基5.例7】工艺品制作

import math

class Stu:
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

w, x, h = map(int, input().split())
V = int(w * x * h)
a = []
for i in range(w):
    a.append([])
    for j in range(x):
        a[i].append([])
        for k in range(h):
            a[i][j].append(1)
#print(a)
T = int(input())
while T > 0:
    w1, x1, h1, w2, x2, h2 = map(int, input().split())
    w1 -= 1
    x1 -= 1
    h1 -= 1
    w2 -= 1
    x2 -= 1
    h2 -= 1
    for i in range(w1, w2+1):
        for j in range(x1, x2+1):
            for k in range(h1, h2+1):
                a[i][j][k] = 0
    T -= 1
ans = 0
for i in range(w):
    for j in range(x):
        for k in range(h):
            if a[i][j][k] == 1:
                ans += 1
print(ans)

P2550 [AHOI2001]彩票摇奖

n = int(input())

a = [int(i) for i in input().split()]
ans = [0, 0, 0, 0, 0, 0, 0]

while n > 0:
    b = [int(i) for i in input().split()]
    temp = 0
    for i in b:
        for j in a:
            if i == j:
                temp += 1
    if temp > 0:
        ans[temp-1] += 1
    b.clear()
    n -= 1
ans.reverse()
for i in range(6):
    print(ans[i], end=' ')
print(ans[6])

P2615 [NOIP2015 提高组] 神奇的幻方

num = int(input())

# 幻方数组hf 【行】【列】
hf = [[] for i in range(num)]

for i in range(num):
    for j in range(num):
        hf[i].append(0)

# 规则代码化

# 定起点“1”
hf[0][num//2] = 1

# 找特定数的位置函数
def findNum(n, hf = hf, num = num):
    row = 0
    column = 0
    for i in range(num):
        try:
            if hf[i].index(n) >= 0:
                row = i
                column = hf[i].index(n)
                break
        except:
            column = 0
    return row, column

# 定其他数
for i in range(1,num*num):
    i = i + 1
    # seat为i-1的位置(行,列)
    seat = findNum(i-1)
    if seat[0] == 0 and seat[1] != num - 1:
        hf[num-1][seat[1]+1] = i
    elif seat[1] == num -1 and seat[0] != 0:
        hf[seat[0]-1][0] = i
    elif seat[0] == 0 and seat[1] == num - 1:
        hf[seat[0]+1][seat[1]] = i
    elif seat[0] != 0 and seat[1] != num-1:
        if hf[seat[0]-1][seat[1]+1] == 0:
            hf[seat[0] - 1][seat[1] + 1] = i
        else:
            hf[seat[0] + 1][seat[1]] = i


# 幻方输出
for i in range(num):
    for j in range(num):
        print(hf[i][j],end=" ")
    print("")

P5730 【深基5.例10】显示屏

n = int(input())
s = input()
a = []
maxn = n * 4
for i in range(5):
    a.append([])
    for j in range(maxn):
        a[i].append('.')

ls = len(s)
for i in range(ls):
    p = i * 4
    if s[i] == '0':
        a[0][p] = a[0][p+1] = a[0][p+2] = a[1][p] = a[2][p] = a[3][p] = a[4][p] = a[1][p+2]= a[2][p+2] = a[3][p+2] = a[4][p+1] = a[4][p+2] = 'X'
    elif s[i] == '1':
        a[0][p+2] = a[1][p+2] = a[2][p+2] = a[3][p+2] = a[4][p+2] = 'X'
    elif s[i] == '2':
        a[0][p] = a[0][p+1] = a[0][p+2] = a[2][p] = a[2][p+1] = a[2][p+2] = a[4][p] = a[4][p+1] = a[4][p+2] = a[1][p+2] = a[3][p] = 'X'
    elif s[i] == '3':
        a[0][p] = a[0][p + 1] = a[0][p + 2] = a[2][p] = a[2][p + 1] = a[2][p + 2] = a[4][p] = a[4][p + 1] = a[4][p + 2] = a[1][p + 2] = a[3][p + 2] = 'X'
    elif s[i] == '4':
        a[0][p] = a[0][p+2] = a[1][p] = a[1][p+2] = a[2][p] = a[2][p+1] = a[2][p+2] = a[3][p+2] = a[4][p+2] = 'X'
    elif s[i] == '5':
        a[0][p] = a[0][p+1] = a[0][p+2] = a[2][p] = a[2][p+1] = a[2][p+2] = a[4][p] = a[4][p+1] = a[4][p+2] = a[1][p] = a[3][p+2] = 'X'
    elif s[i] == '6':
        a[0][p] = a[0][p + 1] = a[0][p + 2] = a[2][p] = a[2][p + 1] = a[2][p + 2] = a[4][p] = a[4][p + 1] = a[4][p + 2] = a[1][p] = a[3][p + 2] = a[3][p] = 'X'
    elif s[i] == '7':
        a[0][p] = a[0][p+1] = a[0][p+2] = a[1][p+2] = a[2][p+2] = a[3][p+2] = a[4][p+2] = 'X'
    elif s[i] == '8':
        a[0][p] = a[0][p + 1] = a[0][p + 2] = a[2][p] = a[2][p + 1] = a[2][p + 2] = a[4][p] = a[4][p + 1] = a[4][p + 2] = a[1][p + 2] = a[3][p + 2] = a[1][p] = a[3][p] = 'X'
    elif s[i] == '9':
        a[0][p] = a[0][p + 1] = a[0][p + 2] = a[2][p] = a[2][p + 1] = a[2][p + 2] = a[4][p] = a[4][p + 1] = a[4][p + 2] = a[1][p + 2] = a[3][p + 2] = a[1][p] = 'X'

for i in range(5):
    for j in range(n*4-1):
        print(a[i][j], end='')
    print()

P1554 梦中的统计

x, y = map(int, input().split())

ans = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

for n in range(x, y+1):
    if n == 0:
        ans[n] += 1
        continue
    while n > 0:
        k = n % 10
        ans[k] += 1
        n //= 10
for i in range(9):
    print(ans[i], end=' ')
print(ans[9])

P2141 [NOIP2014 普及组] 珠心算测验

n = int(input())
a = [int(i) for i in input().split()]
a.sort()
vis = []
for i in range(20001):
    vis.append(True)
ans = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        sum = a[i] + a[j]
        for k in range(j+1, n):
            if sum == a[k] and vis[sum]:
                ans += 1
                vis[sum] = False

print(ans)

P1614 爱与愁的心痛

n, m = map(int, input().split())

a = []
ans = []
for i in range(n):
    a.append(int(input()))

for i in range(n-m+1):
    sum = 0
    for j in range(i, i+m,):
        sum += a[j]
    ans.append(sum)
ans.sort()
print(ans[0])

P2911 [USACO08OCT]Bovine Bones G

x, y, z = map(int, input().split())

ans = []
for i in range(1, x+1):
    for j in range(1, y+1):
        for k in range(1, z+1):
            sum = i + j + k
            ans.append(sum)

ans.sort()
#print(ans)
l = len(ans)

maxx = 1
cnt = 1
fin = 0

for i in range(1, l):
    if ans[i] == ans[i-1]:
        cnt += 1
    else:
        if cnt > maxx:
            maxx = cnt
            fin = ans[i-1]
        cnt = 1
        #print(cnt, maxx, ans[i])
print(fin)

P1161 开灯

import math
import sys

T = int(input())

f = []
for i in range(2000001):
    f.append(False)


while T > 0:
    a, t = map(float, input().split())
    for i in range(int(t)):
        x = math.floor(a * (i+1))
        f[x] = ~f[x]
    T -= 1
for i in range(1, 2000001):
    if f[i]:
        print(i)
        sys.exit(0)

P5731 【深基5.习6】蛇形方阵

n = int(input())
lst = [[0 for _ in range(n)] for _ in range(n)]
x,y = 0,0
b = 1
for i in range(1, n**2+1):
    lst[x][y] = '{:>3}'.format(i)
    if -1 < y+b < n and lst[x][y+b] == 0:
        y += b
    elif x+b < n and lst[x+b][y] == 0:
        x += b
    else:
        b = -b
        y += b
for i in lst:
    print(*i, sep='')

P5732 【深基5.习7】杨辉三角

n = int(input())
a = []
for i in range(n):
    a.append([])
    for j in range(n):
        a[i].append(0)
#print(a)
for i in range(n):
    a[i][0] = 1
    a[i][i] = 1
for i in range(1, n):
    for j in range (1, n):
        a[i][j] = a[i-1][j-1] + a[i-1][j]

for i in range(n):
    for j in range(i):
        print(a[i][j], end=' ')
    print(a[i][i])

P1789 【Mc生存】插火把

n, m , k = map(int, input().split())
sum = n * n
mp = []
for i in range(n):
    mp.append([])
    for j in range(n):
        mp[i].append(False)
while m > 0:
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    for i in range(x-2, x+3, 1):
        if i < 0 or i >= n:
            continue
        else:
            mp[i][y] = True
    for i in range(y-2, y+3, 1):
        if i < 0 or i >= n:
            continue
        else:
            mp[x][i] = True
    for i in range(x-1, x+2, 1):
        for j in range(y-1, y+2, 1):
            if i < 0 or i >= n or j < 0 or j >= n:
                continue
            else:
                mp[i][j] = True
    m -= 1
while k > 0:
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    for i in range(x-2, x+3, 1):
        for j in range(y-2, y+3, 1):
            if i < 0 or i >= n or j < 0 or j >= n:
                continue
            else:
                mp[i][j] = True
    k -= 1
cnt = 0
for i in range(n):
    for j in range(n):
        if mp[i][j]:
            cnt += 1

# for i in range(n):
#     for j in range(n):
#         print(mp[i][j], end='  ')
#     print()


print(sum - cnt)

P1319 压缩技术

a = [int(i) for i in input().split()]
n = a[0]
sum = n * n
la = len(a)
flag = False
ans = []
for i in range(1, la, 1):
    num = a[i]
    for j in range(num):
        if ~flag:
            ans.append(0)
        else:
            ans.append(1)
    flag = ~flag
cnt = 0
while cnt < sum:
    for i in range(n-1):
        print(ans[cnt], end='')
        cnt += 1
    print(ans[cnt])
    cnt += 1

P1320 压缩技术(续集版)

s = input().strip()
n = len(s)
v = 1
while v != n:
    s += input().strip()
    v += 1

print(n, end=' ')
ls = len(s)
temp = 1
if s[0] == '1':
    print(0, end=' ')
for i in range(1, ls):
    if s[i] != s[i-1]:
        print(temp, end=' ')
        temp = 1
    else:
        temp += 1
print(temp)

P1205 [USACO1.2]方块转换 Transformations

n = int(input())
a = ['']*n
b = ['']*n
for i in range(n):
    a[i] = list(input().strip())
for i in range(n):
    b[i] = list(input().strip())

def d1(a,n):
    a1 = ['']*n
    for i in range(n):
        a1[i] = ['']*n
    for i in range(n):
        for j in range(n):
            a1[j][n-1-i]=a[i][j]
    return a1

def d2(a,n):
    a2 = ['']*n
    for i in range(n):
        a2[i] = ['']*n
    for i in range(n):
        for j in range(n):
            a2[n-1-i][n-1-j]=a[i][j]
    return a2

def d3(a,n):
    a3 = ['']*n
    for i in range(n):
        a3[i] = ['']*n
    for i in range(n):
        for j in range(n):
            a3[n-1-j][i]=a[i][j]
    return a3

def d4(a,n):
    a4 = ['']*n
    for i in range(n):
        a4[i] = ['']*n
    for i in range(n):
        for j in range(n):
            a4[i][n-1-j]=a[i][j]
    return a4

if d1(a,n) == b:
    print(1)
elif d2(a,n) == b:
    print(2)
elif d3(a,n) == b:
    print(3)
elif d4(a,n) == b:
    print(4)
elif d1(d4(a,n),n) == b or d2(d4(a,n),n) == b or d3(d4(a,n),n) == b:
    print(5)
elif a==b:
    print(6)
else:
    print(7)

你可能感兴趣的:(Python学习,python,开发语言,后端)