数串——牛客刷题

题目描述:

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

题目分析:

首先,我们将输入的数字转成字符串存入数组,方便下一步对字符串进行拼接比较大小。

其次,我们通过冒泡排序(或者其他排序)将数组中的元素从大到小排序。任意两个数之间的大小定义:AB>BA,则A>B。

最后,将数组中各元素依次拼接即可得到最大的数字。

代码实现:

import java.util.Scanner;
public class numStr {
	public static void main(String[]args){
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		scan.nextLine();
		String numIn=scan.nextLine();
		String[] num=numIn.split(" ");
		//冒泡排序的变形
		for(int i=0;i

 

你可能感兴趣的:(leedcode)