Given a collection of number segments, you are supposed to recover thesmallest number from them. For example, given {32, 321, 3214, 0229, 87},we can recover many numbers such like 32-321-3214-0229-87 or0229-32-87-321-3214 with respect to different orders of combinations ofthese segments, and the smallest number is 0229-321-3214-32-87.
Input Specification:
Each input file contains one test case. Each case gives a positiveinteger N (<=10000) followed by N number segments. Each segmentcontains a non-negative integer of no more than 8 digits. All thenumbers in a line are separated by a space.
Output Specification:
For each test case, print the smallest number in one line. Do not outputleading zeros.
Sample Input:
5 32 321 3214 0229 87
Sample Output:
22932132143287
#include <iostream> #include <cstdio> #include <algorithm> #include <string> #define MaxSize 10010 using namespace std; bool cmp(string A,string B) { return A+B<B+A; } int main() { string str[MaxSize]; string answer; int i,n; scanf("%d",&n); for(i=0;i<n;i++) cin>>str[i]; sort(str,str+n,cmp); for(i=0;i<n;i++) answer+=str[i]; /* while(answer.size()!=0 &&answer[0]=='0') { answer.erase(answer.begin()); } if(answer.size()!=0) cout<<answer; */ i=0; while(i<answer.size()) { if(answer[i]!='0') break; i++; } if(i<answer.size()) printf("%s",answer.c_str()+i); //此处使用answer+i报错,注意要使用指针, else printf("0"); return 0; }