zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
        
    }
  • 相关阅读:
    预备作业2
    我期待的师生关系
    20162317袁逸灏 第四周实验报告
    《程序设计与数据结构》第4周学习总结
    《程序设计与数据结构》第3周学习总结
    20162317 《程序设计与数据结构》第2周学习总结
    学号20162317《程序设计与数据结构》第1周学习总结
    预备作业03
    预备作业02
    20162317袁逸灏
  • 原文地址:https://www.cnblogs.com/bmbi/p/5248110.html
Copyright © 2011-2022 走看看