#include<iostream> #include<vector> #include<algorithm> #include<cstring> using namespace std; struct str{ char s[16]; int index; }; bool cmp(str a,str b) { int len1 = strlen(a.s); int len2 = strlen(b.s); if(len1 == 0 || len2 == 0) return false; int k = 0; while(k < len1 && k < len2) { if(a.s[k] != b.s[k]) { return a.s[k] < b.s[k]; } k++; } if(len1 == len2) return true; int temp = 0; while(k < len1) { if(b.s[temp % len2] != a.s[k]) { return a.s[k] < b.s[temp % len2]; } temp++; k++; } while(k < len2) { if(b.s[k] != a.s[temp % len1]) { return a.s[temp % len1] < b.s[k]; } temp++; k++; } return len1 > len2; } bool test(char * a, char *b) { if(strlen(a) == 0) { if(b[0] == ‘0‘ && strlen(b) == 1) return false; if(b[0] == ‘0‘) { strcpy(a,b+1); }else { strcpy(a,b); } return true; }else { str a_temp, b_temp; strcpy(a_temp.s,a); strcpy(b_temp.s,b); if(a[0] == ‘0‘) { strcpy(a_temp.s,a+1); }if(b[0] == ‘0‘) { strcpy(b_temp.s,b+1); } if(cmp(b_temp,a_temp)) { strcpy(a, b_temp.s); return true; } return false; } } int main() { vector<str>vec, vec_non_zero; int n, _min = 1e9, index; str temp; char ch[16]; ch[0] = ‘\0‘; cin >> n; for(int i = 0; i < n; i++) { cin >> temp.s; temp.index = i; vec.push_back(temp); if(test(ch,temp.s)) { index = i; } } sort(vec.begin(), vec.end(), cmp); cout << ch; for(int i = 0; i < vec.size(); i++) { if(vec[i].index == index) continue; cout << vec[i].s; } }
上述为错误代码。 错误原因 同 https://blog.csdn.net/xyt8023y/article/details/46473243 一样
#include<iostream> #include<vector> #include<algorithm> #include<cstring> using namespace std; vector<string> temp; bool cmp(string a, string b) { return a+b < b+a; } int main() { int num, i; string str, tes; cin >> num; for(i = 0; i < num;i++) { cin >> str; temp.push_back(str); } sort(temp.begin(), temp.end(), cmp); for(int i = 0; i <num;i++) { tes += temp[i]; } i = 0; while(i < tes.length() && tes[i] == ‘0‘) i++; if(i == tes.length()) printf("0"); else { for(; i<tes.length(); i++) { cout << tes[i]; } } }
上述为正确代码。 //https://blog.csdn.net/sinat_29278271/article/details/48047877
总结:cmp 排序 也可以考虑组合情况 ,这样就不需要考虑每一位的情况
原文:https://www.cnblogs.com/dcklm/p/10348928.html