Sorting Photo Files

题目1 : Sorting Photo Files

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

You have a lot of photos whose file names are like:

beijing1  

beijing10

beijing8

shanghai233

As you can see, all names have exactly two parts: the string part and the number part. If you sort the files in lexicographical order "beijing10" will go before "beijing8". You do not like that. You want to sort the files first in lexicographical order of the string part then in ascending order of the number part. Can you write a program to work it out?  

输入

The first line contains an integer N denoting the number of files. (1 <= N <= 100)  

The following N lines each contain a file name as described above.  

It is guaranteed that the number part has no leading zeroes and is no more than 1000000.

输出

The sorted files one per line.

样例输入

4  
beijing1  
beijing10 
beijing8 
b233

样例输出

b233  
beijing1  
beijing8  
beijing10

import java.util.Arrays;
import java.util.Scanner;


class node implements Comparable<node>{
    public String string;
    public int val;
    public node(String s,int i){
        string = s;
        val = i;
    }
    @Override
    public int compareTo(node b){
        if(this.string.equals(b.string)) return this.val - b.val;
        else return this.string.compareTo(b.string);
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        node[] nodes = new node[n];
//        Pattern p = Pattern.compile("(a-zA-Z)(0-9)");
        for(int i = 0; i < n; i++) {
            String temp = in.next();
//            Matcher m = p.matcher(temp);
//            String[] substrings = temp.split("(a-zA-Z)(0-9)");
            String t_s = temp.replaceAll("[^(a-zA-Z)]","");
            int t_v = Integer.valueOf(temp.replaceAll("[^(0-9)]",""));
            node t_node = new node(t_s,t_v);
            nodes[i] = t_node;
        }
        Arrays.sort(nodes);
        for(int i = 0; i < nodes.length; i++)
            System.out.println(nodes[i].string+String.valueOf(nodes[i].val));
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_38970751/article/details/85528137