UVa 10905

题目

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1846


题意

n个数字按照字符串方式直接组合成大数字,问最大组合成多少

思路

排序

感想

1. 因为脑洞的原因以为只要把所有数的数位填到一样长就好了,比如27,273,72,723把27填到272,72填到727,就能把它们顺利排出来。结果这样无法区别345, 3453这种情况和543, 5435这种情况。总之为了这种脑洞花费了过多时间。

2. 后来对每个元素x,按照x / (basex - 1)排序

3. 最后发现每个元素可能非常长。。。

代码

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#define LOCAL_DEBUG
using namespace std;
typedef pair<double, long long> MyPair;
const int MAXN = 51;
long long base[19];
string a[MAXN];
bool cmp(string s1, string s2) {
    return s1 + s2 < s2 + s1;
}

int main() {
#ifdef LOCAL_DEBUG
    freopen("C:\Users\Iris\source\repos\ACM\ACM\input.txt", "r", stdin);
    freopen("C:\Users\Iris\source\repos\ACM\ACM\output.txt", "w", stdout);
#endif // LOCAL_DEBUG
    int T;
    base[0] = 1;
    for (int i = 1; i < 19; i++)base[i] = base[i - 1] * 10;
    int n;
    for (int ti = 1;cin>>n && n; ti++) {
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        sort(a, a + n, cmp);
        for (int i = n - 1; i >= 0;i--) {
            cout << a[i];
        }
        cout << endl;
    }

    return 0;
}
View Code
原文地址:https://www.cnblogs.com/xuesu/p/10467498.html