每日AC -数串

来源:互联网 发布:淘宝刀具规则 编辑:程序博客网 时间:2024/05/19 20:56

每日AC -数串


设有n个正整数,将他们连接成一排,组成一个最大的多位整数。
如:n=3时,3个整数13,312,343,连成的最大整数为34331213。
如:n=4时,4个整数7,13,4,246连接成的最大整数为7424613。

输入描述:
有多组测试样例,每组测试样例包含两行,第一行为一个整数N(N<=100),第二行包含N个数(每个数不超过1000,空格分开)。


输出描述:
每组数据输出一个表示最大的整数。
示例1

输入

212 12347 13 4 246

输出

123127424613

AC代码:

import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.Scanner;/** * 类说明 *  * <pre> * Modify Information: * Author        Date          Description * ============ =========== ============================ * DELL          2017年8月16日    Create this file * </pre> *  */public class GetMaxInteger {    /**     * @param args     */    public static void main(String[] args) {        ArrayList<Integer> arr = new ArrayList<Integer>();        Scanner cin = new Scanner(System.in);        int n = cin.nextInt();        String str = "";        for (int i = 0; i < n; i++) {            int num = cin.nextInt();            arr.add(num);        }                // 不要试图使用 arr.sort()可能Java 1.7编译不过去        Collections.sort(arr,new comarator());        for (int i = 0; i < arr.size(); i++) {            str += arr.get(i) + "";        }        System.out.println(str);    }    public static class comarator implements Comparator<Integer> {        @Override        public int compare(Integer o1, Integer o2) {            // int max1 = getMaxInteger(o1);            // int max2 = getMaxInteger(o2);\            // 逆字典序排序就可以 为啥字典序排序就可以,这个和容易理解  123 12  是12312 还是12123 哪个字典序更高一目了然            String a = String.valueOf(o1);            String b = String.valueOf(o2);            return -(a + b).compareTo(b + a);        }    }}




原创粉丝点击