Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. //Java
  2. import java.util.Scanner;
  3.  
  4. class uva299{
  5. public static void main(String args[]){
  6. Scanner sc=new Scanner(System.in);
  7.  
  8. int cases=sc.nextInt();
  9. for(int i=0;i<cases;i++){
  10. int L=sc.nextInt();
  11. int arr[]=new int[L];
  12. for(int j=0;j<L;j++){
  13. arr[j]=sc.nextInt();
  14. }
  15.  
  16. //使用氣泡排序法去計算需交換幾次。
  17. int count=0;
  18. for(int j=0;j<L-1;j++){
  19. for(int k=0;k<L-j-1;k++){
  20. if(arr[k]>arr[k+1]){
  21. int temp=arr[k];
  22. arr[k]=arr[k+1];
  23. arr[k+1]=temp;
  24. count++;
  25. }
  26. }
  27. }
  28.  
  29. //Output
  30. System.out.println("Optimal train swapping takes "+count+" swaps.");
  31. }
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement