组成最大数

题目: 小组中每位都有一张卡片卡片上是6位内的正整数;将卡片连起来可以组成多种数字;计算组成的最大数字。

输入4589,101,41425,9999,输出9999458941425101,题目极限是需要对25个6位数进行拼接。时间限制1s 空间限制64MB;

思路,将字段两两比较,大的往前移动

代码

`

public class 组成最大数 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String [] str=sc.nextLine().split(",");
        for (int i = 0; i < str.length; i++) {
            for (int j = i+1; j <str.length ; j++) {
                if (Integer.parseInt(str[i]+str[j])<Integer.parseInt(str[j]+str[i])){
                    String temp=str[i];
                    str[i]=str[j];
                    str[j]=temp;
                }
            }
        }
        String res="";
        for (int i = 0; i < str.length; i++) {
            res+=str[i];
        }
        System.out.println(res);
    }
}

`

posted @ 2023-11-01 15:06  约拿小叶  阅读(29)  评论(0编辑  收藏  举报