UVA 229 Java

  1. import java.util.*;
  2. import static java.lang.System.*;
  3.  
  4. public class main{
  5. public static void main(String[] args) {
  6. Scanner sc=new Scanner(System.in);
  7. int cases = sc.nextInt();
  8. for(int t = 0; t < cases; t++)
  9. {
  10. int n = sc.nextInt();
  11. int[] datas = new int[n];
  12. for(int i = 0; i < n; i++)
  13. datas[i] = sc.nextInt();
  14. int count = 0;
  15. for(int i = 0; i < n-1; i++)
  16. for(int j = 0; j < n-1; j++)
  17. {
  18. if(datas[j] > datas[j+1])
  19. {
  20. int temp = datas[j+1];
  21. datas[j+1] = datas[j];
  22. datas[j] = temp;
  23. count++;
  24. }
  25. }
  26. System.out.printf("Optimal train swapping takes %d swaps.\r\n", count);
  27. }
  28. }
  29. };
  30.  

留言

這個網誌中的熱門文章

C# 井字遊戲