寻找身高相近的小朋友

while True:
    a = input()
    b = input()

    xmh1 = int(a.split()[0])
    n = int(a.split()[1])
    hlist = list(map(lambda x: int(x), b.split()))
    print(hlist)
    for i in range(n):
        for j in range(0, n - i - 1):
            if abs(hlist[j] - xmh1) > abs(hlist[j + 1] - xmh1):
                hlist[j], hlist[j + 1] = hlist[j + 1], hlist[j]
            if abs(hlist[j] - xmh1) == abs(hlist[j + 1] - xmh1) and hlist[j] > hlist[j + 1]:
                hlist[j], hlist[j + 1] = hlist[j + 1], hlist[j]
    endlist = list(map(lambda x: str(x), hlist))
    print(" ".join(endlist))
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

围棋的气

while True:
    try:
        black=list(map(int,input().split()))
        white=list(map(int,input().split()))
        black1=[]
        white1=[]
        for i in range(0,len(black),2):
            co=[black[i],black[i+1]]
            black1.append(co)
        for i in range(0,len(white),2):
            co=[white[i],white[i+1]]
            white1.append(co)
        plate=[[0 for j in range(19)] for i in range(19)]
        for chess in black1:
            plate[chess[0]][chess[1]]=1
        for chess in white1:
            plate[chess[0]][chess[1]]=2
        def qi(x,plate):
            cnt=0
            for i in range(19):
                for j in range(19):
                    if plate[i][j]==0:
                        if i+1<19 and plate[i+1][j]==x:
                            cnt+=1
                        elif i-1>=0 and plate[i-1][j]==x:
                            cnt+=1
                        elif j+1<19 and plate[i][j+1]==x:
                            cnt+=1
                        elif j-1>=0 and plate[i][j-1]==x:
                            cnt+=1
                    else:
                        continue
            return cnt
        black_cnt=qi(1,plate)
        white_cnt=qi(2,plate)
        print('{} {}'.format(black_cnt,white_cnt))
    except:
        break
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.

用连续自然数之和来表达整数

import sys
 
t = int(input())
def result(t):
    arr = [i+1 for i in range(t)]
    res = []
    l = 0
    r = 1
    sum = arr[l]
    while l < t:
        if sum > t:
            sum -= arr[l]
            l += 1
        elif sum == t:
            res.append(arr[l:r])
            sum -= arr[l]
            l += 1
            if r >= t:
                break
            else:
                sum += arr[r]
                r += 1
        else:
            sum += arr[r]
            r += 1
    res.sort(key = lambda x:len(x))
    for i in res:
        print(f"{t}={'+'.join(map(str, i))}")
    print(f"Result:{len(res)}")
result(t)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.