This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scn = new Scanner(System.in); | |
int t = scn.nextInt(); | |
while(t--!=0) { | |
int n=scn.nextInt(),arr[]=new int [n],count=0; | |
for(int i=0;i<n;i++){ | |
arr[i]=scn.nextInt(); | |
} | |
//泡沫排序 | |
for(int i=0;i<n-1;i++) { | |
for(int j=i+1;j<n;j++){ | |
if(arr[i]>arr[j]) { | |
int temp=arr[i]; | |
arr[i]=arr[j]; | |
arr[j]=temp; | |
count++; | |
} | |
} | |
} | |
System.out.printf("Optimal train swapping takes %d swaps.\n",count); | |
} | |
} | |
/*題目:Q299: Train Swapping | |
作者:1010 | |
時間:西元 2017 年9 月 */ | |
} |
106.09.26 CPE第二題
這題就用你喜歡的排序法實作並用一個count計算交換次數就行囉!