2014-09-10

299 - Train Swapping


題目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=235


import java.util.*;
import java.lang.*;


public class Main{
 
 public  static void main(String[] args) {
  Scanner k = new Scanner(System.in);
  int times = k.nextInt();
  while(times--!=0){
   int trainLen = k.nextInt();
   int[] train = new int[trainLen];
   for(int i=0 ; i<trainLen ; i++){
    int input = k.nextInt();
    train[i]=input;
   }
   int count=0;
   for(int i=0 ; i<trainLen ; i++){
    for(int j=i+1 ; j<trainLen ; j++){
     if(train[i]>train[j]){
      int temp = train[i];
      train[i]=train[j];
      train[j]=temp;
      count++;
     }
    }
   }
   System.out.printf("Optimal train swapping takes %d swaps.%n",count);
  }
  
 }
 
 
 }


沒有留言:

張貼留言

(VM) Ubuntu enable ssh

OS版本:14.04 LTS 相關指令: sudo apt-get install openssh-server Port forwarding設定 : 以virtual box為例子,網路->進階->連接阜轉送(port forwarding) ...