哪些网站是做采购的/竞价关键词排名软件
题目大意:给n个数字,将它们重新排序得到一个最大的数字,好像给出123 456 789 拼为 789456123 最大
解题思路:这题可以算是一个排序题,不过排序的规则有讲究
如果想用字典序排序,显然错了,好像999123 999 , 按字典序排序999123在前面,得到的数字为999123999 , 显然没有不够999999123 大
一种简单的思想,假设我们有n个串,排序了,得到了最优序列,那么我们任意找两块,A和B,我们试图去交换这两块的位置,结果是什么,结果是一定得到的数字一定 <= 最大值 , 这用反证就能证明,如果交换了能更大,那我们早就交换了。所以在排序的时候,比较这个步骤就看 A+B > B+A 是否成立,成立的话A在前面,其中+是表示两个串的连接,在整个排序过程中,都以这种规则来排,排完后就是最优的
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;bool cmp (string a, string b) {return a + b > b + a;
}int main() {int n;string num[55];while (cin >> n && n) {for (int i = 0; i < n; i++)cin >> num[i];sort(num, num + n, cmp);for (int i = 0; i < n; i++)cout << num[i];cout << endl;}return 0;
}