数字接力赛
欢迎进入我的C语言世界
题目
Problem Description
给定两个数字,可将它们拼接在一起形成一个新的数字,称之为数字接力。
现在进行数字接力赛,给定n个数,看哪种拼接方式形成的数字最大。例如1,2,3三个数字,六种可能的拼接结果是:123,132,213,231,312,321。容易发现最大的数字是321。
Input
本题有多组输入数据,你必须处理到EOF为止。
每组数据第一行是一个正整数n(1≤n≤100000)。接下来n行每行一个非负整数m (0≤m<231)。
Output
输出一行,表示由n个数拼接而成的最大数字。
Sample Input
3
1
2
3
Sample Output
321
答案
下面展示 实现代码
。
#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
using namespace std;
string s[100007];
bool my_cmp(string a,string b)
{
return ((a+b) > (b+a));
}
int main()
{
int n;
while(scanf("%d",&n) != EOF)
{
for(int i = 0; i < n; i++)
{
cin >> s[i];
}
sort(s,s+n,my_cmp);
for(int i = 0; i < n; i++)
{
cout << s[i];
}
cout << endl;
}
return 0;
}
本题感悟
本块内容可能来自课本或其他网站,若涉及侵权问题,请联系我进行删除,谢谢大家啦~
- 我一开始用gun c++交的时候超时了,但是Visual C++就通过了
- 本题中要注意的地方是my_cmp函数的书写
- string[100007] 中 string[i]是一个string的串
以上。