编程题解——数串

数串

题目描述

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

输入描述

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

输出描述:

每组数据输出一个表示最大的整数

实例:

输入:

2
12 123
4
7 13 4 246

输出:

12312
7424613

该题笔记:

在此附上源代码java版


import java.util.*;
 
/**
 *
 * @author 林小白
 *
 */
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                list.add(in.nextInt());
            }
            Collections.sort(list, new Comparator<Integer>() {
 
                @Override
                public int compare(Integer o1, Integer o2) {
                    String s1 = String.valueOf(o1);
                    String s2 = String.valueOf(o2);
                    return (s2 + s1).compareTo(s1 + s2);
                }
            });
            for (int i = 0; i < list.size(); i++) {
                System.out.print(list.get(i));
            }
            System.out.println();
        }
    }
}

就该题仅对Collections中sort方法Comparator的重写做出以下笔记

详情可以点击此

以下程序简单说明

import java.util.*;
//以下是学生类Student定义,有点类似C语言的结构体啊!^_^
class Student {
	public int s_no;
	public String s_name;
	public int s_class;
}
 
public class compareTest {
	public static void main(String[] args) {
		//存放学生类的动态数组的初始化
		ArrayList<Student> studentArr = new ArrayList<Student>();
		Student s1 = new Student();
		s1.s_no = 3;
		s1.s_name = "a";
		s1.s_class = 102;
		studentArr.add(s1);
		Student s2 = new Student();
		s2.s_no = 2;
		s2.s_name = "b";
		s2.s_class = 101;
		studentArr.add(s2);
		Student s3 = new Student();
		s3.s_no = 1;
		s3.s_name = "c";
		s3.s_class = 103;
		studentArr.add(s3);
		//初始化之后先打印以下这个动态数组
		System.out.println("排序前:");
		for (int i = 0; i < studentArr.size(); i++) {
			System.out
					.println("我是" + studentArr.get(i).s_class + "班的"
							+ studentArr.get(i).s_name + "学号是"
							+ studentArr.get(i).s_no);
		}
		//对于Comparator接口的重写
		//这个接口就一个抽象函数,给出的参数与返回值都是定死的。
		Collections.sort(studentArr, new Comparator<Object>() {
			public int compare(Object o1, Object o2) {
				//你首先设置你要比较的东西
				//具体是把参数中的Object强制转换成你要比较的东西,这里是两个Student类
				//这里的s1,s2与上面的s1,s2一点关系都没有,只是抽象的前者与后者的关系
				Student s1 = (Student) o1;
				Student s2 = (Student) o2;
				//如果前者的学号大于后者的学号,就是前者大于后者,返回1系统就会识别是前者大于后者
				if (s1.s_no > s2.s_no) {
					return 1;
				}
				//小于同理
				if (s1.s_no < s2.s_no) {
					return -1;
				}
				//如果返回0则认为前者与后者相等
				return 0;
			}
		});
		//比较完毕再输出以学号排序之后的结果
		System.out.println("按学号排序后:");
		for (int i = 0; i < studentArr.size(); i++) {
			System.out
					.println("我是" + studentArr.get(i).s_class + "班的"
							+ studentArr.get(i).s_name + "学号是"
							+ studentArr.get(i).s_no);
		}
		//以下是以班级排序的过程
		Collections.sort(studentArr, new Comparator<Object>() {
			public int compare(Object o1, Object o2) {
				Student s1 = (Student) o1;
				Student s2 = (Student) o2;
				if (s1.s_class > s2.s_class) {
					return 1;
				}
				if (s1.s_class < s2.s_class) {
					return -1;
				}
				return 0;
			}
		});
		System.out.println("按班级排序后:");
		for (int i = 0; i < studentArr.size(); i++) {
			System.out
					.println("我是" + studentArr.get(i).s_class + "班的"
							+ studentArr.get(i).s_name + "学号是"
							+ studentArr.get(i).s_no);
		}
	}
}

最后附上大佬6行代码解决此题

使用的是python语言

在此附上源代码

import sys
for i, v in enumerate(sys.stdin.readlines()):
    if i % 2 == 1:
        v = v.strip().split()
        v.sort(cmp=lambda x, y: cmp(x + y, y + x), reverse=True)
        print('0' if v[0] == '0'else "".join(v))

此程序仅对于python2.7,由于有内置的cmp函数,所以这道非常容易

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值