洛谷题单python解【入门4】数组

  • P1428 小鱼比可爱

n=int(input())
fish=list(map(int,input().split()))
results=[0]*n
for i in range(n):
    for j in range(i):
        if fish[j]<fish[i]:
           results[i]+=1
print(' '.join(map(str,results)))

  • P1427 小鱼的数字游戏

numbers=list(map(int,input().split()))
if numbers[-1]==0:
    numbers.pop()
numbers_reserve=numbers[::-1]
print(' '.join(map(str,numbers_reserve)))

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

n=int(input())
result=[]
result.append(n)
while n!=1:
    if n%2==1:
        n=n*3+1
        result.append(n)
    else:
        n=n//2
        result.append(n)
result_reserve=result[::-1]
print(' '.join(map(str,result_reserve)))
    

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

l,m=map(int,input().split())
regions=[tuple(map(int,input().split())) for _ in range(m)]

trees=[1]*(l+1)
for u, v in regions:
    for i in range(u,v+1):
        if 0<=i<=l:
            trees[i]=0
remaining_trees=sum(trees)
print(remaining_trees)

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

N=int(input())
student=[list(map(int,input().split())) for _ in range(N)]
n=0
for i in range(N):
    for j in range(i+1,N):
        chinese=abs(student[i][0]-student[j][0])
        math=abs(student[i][1]-student[j][1])
        english=abs(student[i][2]-student[j][2])
        total=abs(sum(student[i])-sum(student[j]))
        if chinese<=5 and math<=5 and english<=5 and total<=10:
            n+=1
print(n)

  • P2550 [AHOI2001] 彩票摇奖

n=int(input())
win_nums=set(map(int,input().split()))
award_num=[0,0,0,0,0,0,0]
for i in range(n):
    ticket_nums=set(map(int,input().split()))
    match_count=len(ticket_nums&win_nums)
    if match_count==7:
        award_num[0]+=1
    elif match_count>=1:
        award_num[7-match_count]+=1
    else:
        continue
        
print(' '.join(str(temp) for temp in award_num))

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

N=int(input().strip())
magic_square=[[0 for _ in range(N)] for _ in range(N)]
row,col=0,N//2
magic_square[row][col]=1

for num in range(2,N*N+1):
    new_row,new_col=(row-1)%N,(col+1)%N
    
    if magic_square[new_row][new_col]:
        row=(row+1)%N
    else:
        row,col=new_row,new_col
        
    magic_square[row][col]=num

for i in range(N):
    print(' '.join(str(magic_square[i][j]) for j in range(N)))

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

digit_patterns={
    '0':['XXX','X.X','X.X','X.X','XXX'],
    '1':['..X','..X','..X','..X','..X'],
    '2':['XXX','..X','XXX','X..','XXX'],
    '3':['XXX','..X','XXX','..X','XXX'],
    '4':['X.X','X.X','XXX','..X','..X'],
    '5':['XXX','X..','XXX','..X','XXX'],
    '6':['XXX','X..','XXX','X.X','XXX'],
    '7':['XXX','..X','..X','..X','..X'],
    '8':['XXX','X.X','XXX','X.X','XXX'],
    '9':['XXX','X.X','XXX','..X','XXX']
}

n=int(input())
numbers=input().strip()
output=['' for _ in range(5)]

for digit in numbers:
    pattern=digit_patterns[digit]
    for i in range(5):
        output[i]+=pattern[i]+'.'

for line in output:
    print(line[:-1])

  • P1554 梦中的统计

M,N=map(int,input().split())
digits=[0,0,0,0,0,0,0,0,0,0]
for i in range(M,N+1):
    temp=str(i)
    for j in temp:
        if j=='0':
            digits[0]+=1
        elif j=='1':
            digits[1]+=1
        elif j=='2':
            digits[2]+=1
        elif j=='3':
            digits[3]+=1
        elif j=='4':
            digits[4]+=1
        elif j=='5':
            digits[5]+=1
        elif j=='6':
            digits[6]+=1
        elif j=='7':
            digits[7]+=1
        elif j=='8':
            digits[8]+=1
        else:
            digits[9]+=1
print(' '.join(map(str,digits)))
            
        

  • P2141 [NOIP 2014 普及组] 珠心算测验

n=int(input())
nums=list(map(int,input().split()))
addition=set()
count=0
for i in range(n):
    for j in range(i+1,n):
        addition.add(nums[i]+nums[j])
for num in nums:
    if num in addition:
        count+=1
print(count)

  • P1614 爱与愁的心痛

n,m=map(int,input().split())
a=[]
for i in range(n):
    a.append(int(input()))
current_sum=sum(a[0:m])
min_sum=current_sum
for i in range(m,n):
    current_sum=current_sum+a[i]-a[i-m]
    if current_sum<min_sum:
        min_sum=current_sum
print(min_sum)
    

  • P2911 [USACO08OCT] Bovine Bones G

s1,s2,s3=map(int,input().split())
max_sum=s1+s2+s3
min_sum=3
a=[0]*(max_sum-min_sum+1)
sides_sum=0
for ss1 in range(1,s1+1):
    for ss2 in range(1,s2+1):
        for ss3 in range(1,s3+1):
            current_sum=ss1+ss2+ss3
            a[current_sum-min_sum]+=1
max_count=max(a)
for i in range(len(a)):
    if a[i]==max_count:
        print(i+min_sum)
        break


  • P1161 开灯

n = int(input())
max_light = 2000000
count = [0] * (max_light + 1)

for _ in range(n):
    a_str, t_str = input().split()
    t = int(t_str)
    x_part, d_part = a_str.split('.')
    x = int(x_part)
    d = int(d_part)
    ai_int = x * 1000000 + d  # 将浮点数转换为整数表示

    for k in range(1, t + 1):
        light_num = (k * ai_int) // 1000000  # 计算实际影响的灯编号
        if light_num <= max_light:
            count[light_num] += 1

# 查找第一个被按奇数次的灯编号
for i in range(1, max_light + 1):  # 从1开始,因为灯的编号从1开始
    if count[i] % 2 == 1:
        print(i)
        break

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

n = int(input())
matrix = [[0] * n for _ in range(n)]
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
current_dir = 0
row, col = 0, 0
matrix[row][col] = 1

for i in range(2, n * n + 1):
    while True:
        next_row = row + directions[current_dir][0]
        next_col = col + directions[current_dir][1]
        if 0 <= next_row < n and 0 <= next_col < n and matrix[next_row][next_col] == 0:
            break
        current_dir = (current_dir + 1) % 4
    row, col = next_row, next_col
    matrix[row][col] = i

for r in matrix:
    print(''.join(f"{num:3d}" for num in r))

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

n=int(input())
triangle=[]
for i in range(n):
    row=[1]*(i+1)
    for j in range(1,i):
        row[j]=triangle[i-1][j-1]+triangle[i-1][j]
    triangle.append(row)
for row in triangle:
    print(' '.join(map(str,row)))

  • P1789 【Mc生存】插火把

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

# 生成火把的偏移量(曼哈顿距离 ≤ 2)
torch_offsets = []
for dx in range(-2, 3):
    for dy in range(-2, 3):
        if abs(dx) + abs(dy) <= 2:
            torch_offsets.append((dx, dy))

# 生成萤石的偏移量(5x5正方形)
stone_offsets = [(dx, dy) for dx in range(-2, 3) for dy in range(-2, 3)]

# 初始化光照矩阵
light = [[False for _ in range(n)] for _ in range(n)]

# 处理火把位置
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1  # 转换为0-based索引
    y -= 1
    for dx, dy in torch_offsets:
        nx = x + dx
        ny = y + dy
        if 0 <= nx < n and 0 <= ny < n:
            light[nx][ny] = True

# 处理萤石位置
for _ in range(k):
    o, p = map(int, input().split())
    o -= 1
    p -= 1
    for dx, dy in stone_offsets:
        no = o + dx
        np = p + dy
        if 0 <= no < n and 0 <= np < n:
            light[no][np] = True

# 统计未被照亮的点数
count = 0
for row in light:
    count += row.count(False)

print(count)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值