2022年3月27日 星期日

【CPE】UVA 10041 (Python)

  • 取中位數
  • 委託者住親戚家
  • 排序(由小到大)(注意不要混入非門牌之數字)


t = int(input())

house = 0
length = 0

def findHouse(houses):
    houses.sort()
    return houses[int(len(houses)/2)]

def findLength(house, houses):
    length = 0
    for i in range(0,len(houses)):
        length += abs(house - houses[i])
    return(length)

for i in range(t):
    houses = [int(x) for x in input().split()]
    houses.pop(0)
    if len(houses)>0:
        house = findHouse(houses)
        length = findLength(house, houses)
    print(length)

0 comments:

張貼留言