洛谷 题单 官方精选 数组 Python实现

目录

    • P1428 小鱼比可爱
    • P1427 小鱼的数字游戏
    • P5727 【深基5.例3】冰雹猜想
    • P1047 [NOIP2005 普及组] 校门外的树
    • P5728 【深基5.例5】旗鼓相当的对手
    • P5729 【深基5.例7】工艺品制作
    • P2550 [AHOI2001]彩票摇奖
    • P2615 [NOIP2015 提高组] 神奇的幻方
    • P5730 【深基5.例10】显示屏
    • P1554 梦中的统计
    • P2141 [NOIP2014 普及组] 珠心算测验
    • P1614 爱与愁的心痛
    • P2911 [USACO08OCT]Bovine Bones G
    • P1161 开灯
    • P5731 【深基5.习6】蛇形方阵
    • P5732 【深基5.习7】杨辉三角
    • P1789 【Mc生存】插火把
    • P1319 压缩技术
    • P1320 压缩技术(续集版)
    • P1205 [USACO1.2] 方块转换 Transformations

P1428 小鱼比可爱

n=int(input())
ls=[int(i) for i in input().split()]
for i in range(n):
    cnt=0
    for j in range(i):
        if ls[j]<ls[i]:
            cnt+=1
    print(cnt,end=" ")

P1427 小鱼的数字游戏

ls=[int(i) for i in input().split()]
ls.pop()
ls=ls[::-1]
for i in ls:
    print(i,end=" ")

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

n=int(input())
ls=[n]
while n!=1:
    if n%2==0:
        n//=2
        ls.append(n)
    else:
        n=n*3+1
        ls.append(n)
ls=ls[::-1]
for i in ls:
    print(i,end=" ")

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

l,m=map(int,input().split())
ls=[1]*(l+1)
for i in range(m):
    a,b=map(int,input().split())
    for i in range(a,b+1):
        ls[i]=0
print(ls.count(1))

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

N=int(input())
ls=[]
for i in range(N):
    a,b,c=map(int,input().split())
    ls.append([a,b,c])
cnt=0
for a in ls:
    i=sum(a)
    for b in ls:
        if b==a:
            continue
        else:
            j=sum(b)
            if abs(j-i)<=10:
                for c in range(3):
                    if abs(a[c]-b[c])>5:
                        break
                else:
                    cnt+=1
print(cnt//2)

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

w,x,h=map(int,input().split())
q=int(input())
ls=[]
for i in range(q):
    sl=[int(i) for i in input().split()]
    ls.append(sl)
sl=[[[1 for i in range(h)] for j in range(x)] for k in range(w)]
for a in ls:
    for i in range(a[0]-1,a[3]):
        for j in range(a[1]-1,a[4]):
            for k in range(a[2]-1,a[5]):
                sl[i][j][k]=0
cnt=0
for i in range(w):
    for j in range(x):
        for k in range(h):
            if sl[i][j][k]==1:
                cnt+=1
print(cnt)

P2550 [AHOI2001]彩票摇奖

n=int(input())
ls=[int(i) for i in input().split()]
di=dict(zip([0,1,2,3,4,5,6],[0,0,0,0,0,0,0]))
for i in range(n):
    sl=[int(i) for i in input().split()]
    cnt=7
    for i in sl:
        if i in ls:
            cnt-=1
    try:
        di[cnt]+=1
    except:
        pass
for i in di.values():
    print(i,end=" ")

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

n=int(input())
m=n//2
ls=[[0 for i in range(n)] for i in range(n)]
def hanshu(ls,n,m):
    ls[0][m]=str(1)
    x,y=2,pow(n,2)
    a,b=0,m
    while x<=y:
        if a==0 and b!=n-1:
            ls[n-1][b+1]=str(x)
            a,b=n-1,b+1
        elif b==n-1 and a!=0:
            ls[a-1][0]=str(x)
            a,b=a-1,0
        elif a==0 and b==n-1:
            ls[a+1][b]=str(x)
            a,b=a+1,b
        else:
            if ls[a-1][b+1]==0:
                ls[a-1][b+1]=str(x)
                a,b=a-1,b+1
            else:
                ls[a+1][b]=str(x)
                a,b=a+1,b
        x+=1
    for i in ls:
        print(" ".join(i))
hanshu(ls,n,m)

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

def hanshu(i,j):
    if i==1 and j in [0,2,3,5,6,7,8,9]:
        return "XXX"
    elif i==1 and j==1:
        return "..X"
    elif i==1 and j==4:
        return "X.X"
    elif i==2 and j in [0,4,8,9]:
        return "X.X"
    elif i==2 and j in [1,2,3,7]:
        return "..X"
    elif i==2 and j in [5,6]:
        return "X.."
    elif i==3 and j==0:
        return "X.X"
    elif i==3 and j in [1,7]:
        return "..X"
    elif i==3 and j in [2,3,4,5,6,8,9]:
        return "XXX"
    elif i==4 and j in [0,6,8]:
        return "X.X"
    elif i==4 and j in [1,3,4,5,7,9]:
        return "..X"
    elif i==4 and j==2:
        return "X.."
    elif i==5 and j in [0,2,3,5,6,8,9]:
        return "XXX"
    elif i==5 and j in [1,4,7]:
        return "..X"
n=int(input())
m=[int(i) for i in input().strip()]
for i in range(1,6):
    x=[]
    for j in m:
        x.append(hanshu(i,j))
    print(".".join(x))

P1554 梦中的统计

M,N=map(int,input().split())
di=dict(zip([0,1,2,3,4,5,6,7,8,9],[0,0,0,0,0,0,0,0,0,0]))
ls=[str(i) for i in range(M,N+1)]
for i in ls:
    if "0" in i:
        di[0]+=i.count("0")
    if "1" in i:
        di[1]+=i.count("1")
    if "2" in i:
        di[2]+=i.count("2")
    if "3" in i:
        di[3]+=i.count("3")
    if "4" in i:
        di[4]+=i.count("4")
    if "5" in i:
        di[5]+=i.count("5")
    if "6" in i:
        di[6]+=i.count("6")
    if "7" in i:
        di[7]+=i.count("7")
    if "8" in i:
        di[8]+=i.count("8")
    if "9" in i:
        di[9]+=i.count("9")
for i in di.values():
    print(i,end=" ")

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

n=int(input())
ls=[int(i) for i in input().split()]
ls.sort()
di=[]
for i in range(n):
    for j in range(i+1,n):
        x=ls[i]+ls[j]
        if x in ls:
            di.append(x)
print(len(set(di)))

P1614 爱与愁的心痛

n,m=map(int,input().split())
ls=[]
for i in range(n):
    ls.append(int(input()))
for i in range(n-m+1):
    if i==0:
        x=sum(ls[i:i+m])
        y=x
    else:
        x=x-ls[i-1]+ls[i+m-1]
        if x<y:
            y=x
print(y)

P2911 [USACO08OCT]Bovine Bones G

a,b,c=map(int,input().split())
d=a+b+c
ls1=[i for i in range(3,d+1)]
ls2=[0]*(d-1)
di=dict(zip(ls1,ls2))
for i in range(1,a+1):
    for j in range(1,b+1):
        for k in range(1,c+1):
            e=i+j+k
            di[e]+=1
print(max(di,key=di.get))

P1161 开灯

from math import floor
n=int(input())
ls=[0]*10000000
for i in range(n):
    a,b=map(eval,input().split())
    for i in range(1,b+1):
        j=floor(i*a)
        if ls[j-1]==0:
            ls[j-1]=1
        else:
            ls[j-1]=0
for i in ls:
    if i==1:
        print(ls.index(i)+1)
        break

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

n=int(input())
ls1=[[0 for i in range(n)] for i in range(n)]
def hanshu(ls1):
    global n
    ls1[0][0]=1
    a,b,c=0,0,1
    d=pow(n,2)
    while c<d:
        c+=1
        if b!=n-1 and ls1[a][b+1]==0 and ls1[a+1][b]==0:
            a,b=a,b+1
            ls1[a][b]=c
        elif a!=n-1 and ls1[a+1][b]==0:
            a,b=a+1,b
            ls1[a][b]=c
        elif b!=0 and ls1[a][b-1]==0:
            a,b=a,b-1
            ls1[a][b]=c
        elif a!=0 and ls1[a-1][b]==0:
            a,b=a-1,b
            ls1[a][b]=c
        else:
            a,b=a,b+1
            ls1[a][b]=c
    return ls1
ls=hanshu(ls1)
for i in ls:
    print(" ",end="")
    for j in i:
        print("{:2d}".format(j),end=" ")
    print("")

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

n=int(input())
ls=[]
for i in range(n):
    sl=[]
    if i==0:
        sl.append(1)
    elif i==1:
        sl.append(1)
        sl.append(1)
    else:
        sl.append(1)
        for j in range(1,i):
            sl.append(ls[i-1][j-1]+ls[i-1][j])
        sl.append(1)
    ls.append(sl)
for i in ls:
    for j in i:
        print(j,end=" ")
    print("")

P1789 【Mc生存】插火把

n,m,k=map(int,input().split())
ls=[[0 for i in range(n)] for i in range(n)]
def hanshu1(x,y,n):
    global ls
    x-=1
    y-=1
    for i in range(5):
        z=y-2+i
        if z<0 or z>n-1:
            pass
        else:
            ls[x][z]=1
    for i in range(5):
        z=x-2+i
        if z<0 or z>n-1:
            pass
        else:
            ls[z][y]=1
    if x+1<=n-1 and y+1<=n-1:
        ls[x+1][y+1]=1
    if x-1>=0 and y-1>=0:
        ls[x-1][y-1]=1
    if x-1>=0 and y+1<=n-1:
        ls[x-1][y+1]=1
    if x+1<=n-1 and y-1>=0:
        ls[x+1][y-1]=1
for i in range(m):
    x,y=map(int,input().split())
    hanshu1(x,y,n)
def hanshu2(a,b):
    a-=1
    b-=1
    for i in range(-2,3):
        if a+i<0 or a+i>n-1:
            pass
        else:
            for j in range(-2,3):
                if b+j<0 or b+j>n-1:
                    pass
                else:
                    ls[a+i][b+j]=1
for i in range(k):
    a,b=map(int,input().split())
    hanshu2(a,b)
cnt=0
for i in ls:
    for j in i:
        if j==0:
            cnt+=1
print(cnt)

P1319 压缩技术

ls=[int(i) for i in input().split()]
x=ls.pop(0)
sl=[]
y=0
for i in ls:
    if y==0:
        for j in range(i):
            sl.append(0)
        y=1
    else:
        for j in range(i):
            sl.append(1)
        y=0
z=0
X=pow(x,2)
for i in range(X):
    print(sl[i],end="")
    z+=1
    if z%x==0:
        print("")

P1320 压缩技术(续集版)

ls=[]
x=[i for i in input().rstrip()]
for i in x:
    ls.append(int(i))
m=len(x)
for i in range(m-1):
    x=[i for i in input().rstrip()]
    for i in x:
        ls.append(int(i))
print(m,end=" ")
y=0
z=0
n=pow(m,2)
for i in range(n):
    if ls[i]==y:
        z+=1
    else:
        print(z,end=" ")
        y=ls[i]
        z=1
print(z)

P1205 [USACO1.2] 方块转换 Transformations

n=int(input())
ls1=[]
for i in range(n):
    sl1=[i for i in input().rstrip()]
    ls1.append(sl1)
ls2=[]
for i in range(n):
    sl2=[i for i in input().rstrip()]
    ls2.append(sl2)
def hanshu1(ls1):
    ls2=[[0 for i in range(n)] for i in range(n)]
    for i in range(n):
        for j in range(n):
            ls2[i][j]=ls1[n-j-1][i]
    return ls2
def hanshu2(ls1):
    ls2=hanshu1(ls1)
    ls3=hanshu1(ls2)
    return ls3
def hanshu3(ls1):
    ls2=hanshu1(ls1)
    ls3=hanshu1(ls2)
    ls4=hanshu1(ls3)
    return ls4
def hanshu4(ls1):
    ls2=[]
    for i in ls1:
        ls2.append(i[::-1])
    return ls2
def hanshu5(ls1,ls6):
    ls2=hanshu4(ls1)
    ls3=hanshu1(ls2)
    ls4=hanshu2(ls2)
    ls5=hanshu3(ls2)
    if ls3==ls6 or ls4==ls6 or ls5==ls6:
        return True
    return False
ls=[]
if hanshu1(ls1)==ls2:
    ls.append(1)
if hanshu2(ls1)==ls2:
    ls.append(2)
if hanshu3(ls1)==ls2:
    ls.append(3)
if hanshu4(ls1)==ls2:
    ls.append(4)
if hanshu5(ls1,ls2)==True:
    ls.append(5)
if ls1==ls2:
    ls.append(6)
if ls==[]:
    print(7)
else:
    print(min(ls))

你可能感兴趣的:(python,算法,开发语言)