EOJ.3530和你在一起

题目简介


有这么 n 个数字,联成一排拼到一起便是我爱你的时间,那么我们会在一起多久呢

例如: n=3 时,3 个整数 13,312,343 联接成的最长时间为: 34331213。
又如: n=4 时,4 个整数 7,13,4,246 联接成的最长时间为: 7424613。

EOJ.3530和你在一起
解法:按字典序来排序,但若两者存在包含关系,则将长度短的排在长度长的前面

# include <bits/stdc++.h>
using namespace std;
bool cmp(string a,string b)
{
	bool f;
	int n,i;
	while(a.length() < b.length())
		a += '0';
	while(b.length() < a.length())
		b += '0';
	for(i = 0;i < a.length();i++)
	{
		if(a[i] != b[i])
			return  a[i] > b[i];
	}
	if(a == b)
		return true; 
}
int main(void)
{
	int n,i;
	cin>>n;
	string a[n],res;
	for(i = 0;i < n;i++)
		cin>>a[i];
	sort(a,a+n,cmp);
	for(i = 0;i < n;i++)
		cout<<a[i];
	return 0; 
}