这道题见过,就是把相加的结果作为比较来排序就行了。注意的是comp函数里面要用const引用。而且c++里的字符串直接操作(读入和相加)也很方便。
#include <iostream> #include <memory.h> #include <string> #include <algorithm> #define LEN 105 using namespace std; string s[LEN]; bool comp(const string &a, const string &b) { string s1 = a + b; string s2 = b + a; return s1 < s2; } int main() { int m; while (cin >> m) { for (int i = 0; i < m; i++) { cin >> s[i]; } sort(s, s+m, comp); for (int i = 0; i < m; i++) { cout << s[i]; } cout << endl; } return 0; }