uva10905
题意:给定n个数字字符串,求他们能组成的最大数字字符串。
做法:用<string>,cmp比较函数比较s+t与t+s即可。
代码:
#include <iostream> #include <string> #include <algorithm> #include <vector> #include <stdio.h> #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){ string s; int i, j; vector<string>vs; for(i=0; i<n; i++){ 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.