Swap Maximum and Minimum Values


Submit solution

Points: 10 (partial)
Time limit: 1.0s
Memory limit: 64M

Authors:
Problem type
Allowed languages
Java 19, Java 8

題目說明

請寫一個 Java 程式,讀取使用者輸入的一個整數陣列。 找出陣列中的最大值與最小值,將這兩個數值的位置互換,其餘數值位置不變。最後印出交換後的陣列。

注意規則

  • 若有多個相同的最大值或最小值,請交換索引值最小(最先出現)的那一個。

  • 即使最大值與最小值是同一個數(例如陣列只有一個元素,或所有元素都相同),程式仍需正常執行(雖然輸出結果看起來可能沒變)。

輸入值的格式

  • 第一行輸入一個整數 N ,代表陣列的長度。

  • 第二行輸入 N 個整數。

輸出值的格式

  • 輸出交換後的陣列元素。

  • 輸出僅一行,無多餘空白或符號。

sample input1

5
10 5 8 20 3

sample output1

10 5 8 3 20

sample input2

6
-5 10 2 10 -9 -9

sample output2

-5 -9 2 10 10 -9

Comments

There are no comments at the moment.