水題之 字符串排序

字符串排序
月神拿到一個新的數據集,其中每個樣本都是一個字符串(長度小於100),樣本的的後六位是純數字,月神需要將所有樣本的後六位數字提出來,轉換成數字,並排序輸出。

月神要實現這樣一個很簡單的功能確沒有時間,作爲好朋友的你,一定能解決月神的煩惱,對吧。
輸入描述:

每個測試用例的第一行是一個正整數M(1<=M<=100),表示數據集的樣本數目

接下來輸入M行,每行是數據集的一個樣本,每個樣本均是字符串,且後六位是數字字符。

輸出描述:

對每個數據集,輸出所有樣本的後六位構成的數字排序後的結果(每行輸出一個樣本的結果)

示例1
輸入
複製

4
abc123455
boyxx213456
cba312456
cdwxa654321

import java.io.*;
import java.util.*;
public class Main{
    public static void main(String[] args) throws IOException{
       BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
       int M = Integer.parseInt(reader.readLine());
        int[] res = new int[M];
        int l = 0;
       for(int i=0;i<M;++i) {
           char[] tmp = reader.readLine().toCharArray();
           int decade = 1;
           int carry = 0;
           for(int j=tmp.length-1;j>tmp.length-1-6;--j) {
               if(Character.isDigit(tmp[j])) {
                   carry+=(tmp[j]-'0')*decade;
                   decade*=10;
               }else{
                   break;
               }
               
           }
           res[l++] = carry;
       }
        reader.close();
        
        
        
        
        Arrays.sort(res);
        for(int j:res) {
            System.out.println(j);
        }
        
        
        
        
        
        
        
        
        
        
        
    }

    
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章