#53689: python解


angus7315197@gmail.com (小章魚)

學校 : 不指定學校
編號 : 288474
來源 : [36.239.186.58]
最後登入時間 :
2025-09-22 23:17:25

#撰寫車廂交換用的函式
def swap(x):
    S = 0
    #排序 把數字大的往後丟
    for i in range(len(x)-1):
        for j in range(len(x)-1-i):
            if x[j] > x[j+1]:
                x[j],x[j+1] = x[j+1],x[j]
                S += 1
            else:
                continue
    #印出答案
    print(f"Optimal train swapping takes {S} swaps.")
 
#變數宣告
TestCase = int(input())
for k in range(TestCase):
    Length = int(input())
    #將多個以空格隔開的整數輸入存成list
    Permutation = list(map(int,input().split()))
    #呼叫swap函式 把list丟進去算
    swap(Permutation)