AGE SORT

AGE SORT

你有所有城市的人的年齡資料,而且這城市的人們都大於1歲,且都不會活超過100歲。現在你有個簡單的任務以升冪去排序所有的年齡

Input

接下來會有很多筆的資料,每筆資料從輸入開始 (0 < n < 2000000)n代表著城市全部的人數,

下一行將會有n個數代表著每個人的年齡,當輸入n=0時將會結束。

Ouput

每筆資料輸出一行n個數代表這城市所有的年齡而且以升冪排序,並且用空白分開。

Sample Input

5

3 4 2 1 5

6

2 3 2 3 1 7

0

Sample Output 

1 2 3 4 5

1 2 2 3 3 7


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class AGE {
    public static void main(String[] args) {
    
        Scanner scan = new Scanner(System.in);
        int num;
        int[] score = null;
        List<Integer> result=new ArrayList<Integer>();
            num = scan.nextInt();
            
    while(num!=0){
    
            score = new int[num];
            for(int i=0;i<num;i++){
                score[i]=scan.nextInt();
            }
            
                sort(score);
        
        for(int i=0;i<score.length;i++){
            result.add(score[i]);
        }
        
        result.add(0);
        num = scan.nextInt();
        
    }
    
    playResult(result);
            
}
    
    
    public static void sort(int[] score){
      for (int i = 0; i < score.length -1; i++){    
               for(int j = 0 ;j < score.length - i - 1; j++){    
                   if(score[j] > score[j + 1]){    
                       int temp = score[j];
                       score[j] = score[j + 1];
                       score[j + 1] = temp;
                  }
              } 
          }
          
    }
    
    public static void playResult(List<Integer> result ){
        for(int i:result){
            if(i!=0){
            System.out.print(i+" ");
            }else{
                System.out.println();
            }
        }
    }
    
}
原文地址:https://www.cnblogs.com/bmbi/p/5248110.html