a225: 明明愛排列

題目:http://zerojudge.tw/ShowProblem?problemid=a225
程式碼:

import java.util.Scanner;
public class a225 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner sc = new Scanner(System.in);
  while(sc.hasNext()){
   int n = sc.nextInt();
   int [] s = new int[n];
   for(int i=0;i<n;i++){
    s[i] = sc.nextInt();
   }
   int temp;
   for(int i=s.length-1;i>0;i--){//小到大
    for(int j=0;j<s.length-1;j++){
     if(s[j]%10 > s[j+1]%10){
      temp = s[j];
      s[j] = s[j+1];
      s[j+1] = temp;
     }else if(s[j]%10 == s[j+1]%10){
      if(s[j]/10 < s[j+1]/10){//改變方向
                 temp = s[j];
                 s[j]=s[j+1];
                 s[j+1]=temp;
      }
     }
    }
   }
   for(int arr : s){
    System.out.print(arr+" ");
   }
   System.out.println();
  }

 }

}

留言

熱門文章