UVA10905 - Children's Game

题目链接  


题意:给定n个正整数,你的任务就是把他们连成一个最大的整数,输出最大的数


思路:贪心排序,将能组合而成的较大的放前面


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 55;

string s[MAXN];

int cmp(const string & a, const string & b) {
    return (a + b) > (b + a);
}

int main() {
    int n;
    while (cin >> n && n) {
        for (int i = 0; i < n; i++)
            cin >> s[i];
        sort(s, s + n, cmp); 
        for (int i = 0; i < n; i++) 
            cout << s[i];
        cout << endl;
    }
    return 0;
}


你可能感兴趣的:(UVA10905 - Children's Game)