思想的话看这里吧。http://blog.csdn.net/sinat_29278271/article/details/48047877
比较巧妙。自己写了好长的判断也没通过。
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
int N;
vector <string> list;
bool cmp(string s1, string s2) {
return s1 + s2 < s2 + s1;
}
int str2int(string s) {
int sum = 0;
for (int i = s.size() - 1; i >= 0; i--) {
sum *= 10;
sum += int(s[i] - '0');
}
return sum;
}
int main() {
cin >> N;
string s, small = "0";
for (int i = 0; i < N; i++) {
cin >> s;
if (str2int(s) == 0)
// cout << str2int(s) << endl;
continue;
list.push_back(s);
}
sort(list.begin(), list.end(), cmp);
#ifdef _DEBUG
//for (int i = 0; i < list.size(); i++) {
// cout << list[i] << endl;
//}
#endif
bool tag = true;
bool tag2 = true;
for (int i = 0; i < list.size(); i++) {
if (i == 0) {
for (int j = 0; j < list[0].size(); j++) {
if (list[0][j] != '0') {//有非零
tag = false;
}
if (tag) {
continue;
}
else {
if (tag2) {
small.clear();
tag2 = false;
}
small.push_back(list[0][j]);
}
}
}
else {
small += list[i];
}
}
cout << small << endl;
return 0;
}