数串

题目描述

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

输入描述:

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

输出描述:

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

输入

2
12 123
4
7 13 4 246

输出

12312
7424613

package main

import (
	"bytes"
	"fmt"
	"sort"
	"strconv"
)

var buf bytes.Buffer

type Block struct {
	data []int
	n    int
}

func (this *Block) Len() int {
	return this.n
}

func (this *Block) Swap(i, j int) {
	this.data[i], this.data[j] = this.data[j], this.data[i]
}

func (this *Block) Less(i, j int) bool {
	p, _ := strconv.Atoi(strconv.Itoa(this.data[i]) + strconv.Itoa(this.data[j]))

	q, _ := strconv.Atoi(strconv.Itoa(this.data[j]) + strconv.Itoa(this.data[i]))

	if p <= q {
		return false
	}

	return true
}

func (this *Block) String() string {
	buf.Reset()
	for i := 0; i < this.n; i++ {
		buf.WriteString(strconv.Itoa(this.data[i]))
	}
	return buf.String()
}

func main() {
	obj := &Block{}
	for {
		_, err := fmt.Scanf("%d", &obj.n)

		if err != nil {
			break
		}
		obj.data = make([]int, obj.n)
		for i := 0; i < obj.n; i++ {
			fmt.Scanf("%d", &obj.data[i])
		}
		sort.Sort(obj)
		fmt.Println(obj.String())
	}
}

  

原文地址:https://www.cnblogs.com/oldBook/p/9761442.html