uva10905 Children's Game
题意给定n个正整数,你的任务是把他们连接成一个最大的整数,比如,123,124,56,90有24种连接方法,最大的结果是9056124123
分析:自己写比较函数cmp(s, t), 比较s+t 与t+s的大小即可。
#include <stdio.h> #include <iostream> #include <string> #include <vector> #include <algorithm> #define zz using namespace std; bool cmp(const string &s, const string &t){ return (s+t) > (t+s); } int main(){ #ifndef zz freopen("in.txt", "r", stdin); #endif int n; while(scanf("%d", &n)!=EOF && n){ int i; vector<string>vs; for(i=0; i<n; i++){ string s; cin >> s; vs.push_back(s); } sort(vs.begin(), vs.end(), cmp); for(i=0; i<vs.size(); i++) cout << vs[i]; cout << endl; } return 0; }
Greatness is never a given, it must be earned.