2022年9月16日 星期五

Python b373: [福州19中]车厢重组

b373: [福州19中]车厢重组 



# -*- coding: utf-8 -*-

#b373: [福州19中]车厢重组



def bubble_sort(nums):

    n = len(nums)

    m=0

    for i in range(n-1):

        for j in range(n-i-1):

            if nums[j] > nums[j+1]:

                print(nums)

                m=m+1

                temp = nums[j]

                nums[j] = nums[j+1]

                nums[j+1] = temp

    print('一个数据,是最少的旋转次数-->' ,m)


nums =[]

print('b373: [福州19中]车厢重组')

while True:

    try:

        n = int(input('车厢总数N--->'))   #


        nums = list(map(int,input("\nEnter the numbers : ").strip().split()))[:n]


        print("排序前 = %s" % nums)

        bubble_sort(nums)

        print("排序後 = %s" % nums)


    except:

        break 



>>> %Run -c $EDITOR_CONTENT

b373: [福州19中]车厢重组

车厢总数N--->4


Enter the numbers : 4 3 2 1

排序前 = [4, 3, 2, 1]

[4, 3, 2, 1]

[3, 4, 2, 1]

[3, 2, 4, 1]

[3, 2, 1, 4]

[2, 3, 1, 4]

[2, 1, 3, 4]

一个数据,是最少的旋转次数--> 6

排序後 = [1, 2, 3, 4]

车厢总数N--->

>>> 

沒有留言:

張貼留言

2026-09 MQTT基本觀念(作業1)

 2026-09  MQTT基本觀念(作業1) 將 執行結果 畫面 錄製約 3分鐘 以上 1) MQTT BOX 動作 畫面  2) Python + TKinter 動作 畫面  3) Wokwi ESP32 動作 畫面  並上傳YT (公開) mail alex9ufo@gm...