2017年8月26日 星期六

UVA299:Train Swapping

  1. import java.util.*;  
  2. class main{  
  3. public static void main(String[] args){  
  4. Scanner sc = new Scanner(System.in);  
  5. int count = sc.nextInt();  
  6. for(int i =0;i<count;i++){  
  7. int[] N = new int[sc.nextInt()];  
  8.    for(int j =0;j<N.length;j++)  
  9.         N[j]= sc.nextInt();  
  10. BubbleSort(N);       
  11. }  
  12. }  
  13. static void BubbleSort(int[] N){  
  14. int count =0;  
  15. for(int i = N.length-1;i>0;i--)  
  16.   for(int j = 0;j<i;j++)  
  17.     if(N[j+1]<N[j]){  
  18.     int tmp = N[j+1] ;  
  19.     N[j+1] = N[j];  
  20.      N[j] = tmp ;  
  21.      count++;  
  22.     }  
  23. System.out.println("Optimal train swapping takes "+count+" swaps.");  
  24. }  
  25.   
  26.   
  27. }  

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。