Cattle customer ordering large integer

Links:
Original title
Source: Cattle-off network

topic

The N number of the longest length reaches 1000 sort.

Enter a description:

输入第一行为一个整数N,(1<=N<=100)
接下来的N行每行有一个数,数的长度范围为1<=len<=1000。
每个数都是一个正数,并且保证不包含前缀零。

Output Description:

可能有多组测试数据,对于每组数据,将给出的N个数从小到大进行排序,输出排序后的结果,每个数占一行。

Example 1
Input

3
11111111111111111111111111111
2222222222222222222222222222222222
33333333

Export

33333333
11111111111111111111111111111
2222222222222222222222222222222222

analysis

At first glance this question on the thought of a lazy ------- method we can use the time element would be inserted into the TreeMap Sorts the elements of this feature.
We BigNumber create a custom class and then implement the Comparable interface. The next thing I think we will understand. Override compareTo method.

return value Show
1 Is greater than the specified number of parameters
-1 It is smaller than the specified number of parameters
0 It is equal to the number of parameters specified

We know TreeMap insert data (custom class) when we will call compareTo method override, and then sort the data based on integer returned. Because the default sort is ascending. According to the above table we can write on it.

If we need to reverse that is descending row. We can deliberately write anti return value. When the return is less than 1, so we will be cheated TreeMap, small by big row. The results led to the reverse order.

The following is a comparison method:

public int compareTo(BigNumber number){
        //1.先按长度比较
        if(s.length() < number.s.length()){
            return -1;
        }else if(s.length() > number.s.length()){
            return 1;
        }else{
            //长度相同,接下来我们得好好比较了
            //思虑:从头开始遍历字符串,模拟字符串的比较规则,我们再偷懒直接调用字符串的方法
            return s.compareTo(number.s);
        }
    }

Code

import java.util.TreeMap;
import java.util.Set;
import java.util.Scanner;
//思路:实现Comparable接口自定义一个比较方法,然后利用TreeMap加入自动排序,最后我们获取出Map的keys即可
class BigNumber implements Comparable<BigNumber>{

    private String s;

    public BigNumber(String s){
        this.s = s;
    }

    public int compareTo(BigNumber number){
        //1.先按长度比较
        if(s.length() < number.s.length()){
            return -1;
        }else if(s.length() > number.s.length()){
            return 1;
        }else{
            //长度相同,接下来我们得好好比较了
            //思虑:从头开始遍历字符串,模拟字符串的比较规则
            return s.compareTo(number.s);
        }
    }

    public String getS(){
        return s;
    }
}
public class Main{
    public static void main(String[] args){
        //输入流
        Scanner in = new Scanner(System.in);
        while(in.hasNextLine()) {
            String n = in.nextLine();  //行数,为了避免nextInt()nextLine()全按字符串处理
            //我们定义一个Map
            TreeMap<BigNumber, String> map = new TreeMap<BigNumber, String>();
            int len = Integer.parseInt(n);
            //输入数字
            for (int i = 0; i < len; i++) {
                String s = in.nextLine();
                map.put(new BigNumber(s), null);
            }

            //添加完毕,获取map集合的keys并输出
            Set<BigNumber> keys = map.keySet();
            for (BigNumber key : keys) {
                System.out.println(key.getS());
            }
        }
    }
}
Published 57 original articles · won praise 11 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42419462/article/details/104707564