Title Description:
Enter an array of positive integers, combine all the numbers in the array into a number, and print the smallest of all the numbers that can be stitched together. For example, enter the array {3,32,321}, then print out the minimum number that these three numbers can be ranked as 321323.
Input:
The input may contain multiple test samples.
For each test case, the first behavior of the input, an integer m (1<=m <=100), represents the number of positive integers entered.
The second line of input includes m positive integers, where each positive integer does not exceed 10000000.
Output:
corresponding to each test case,
The smallest number that the output m number can be.
Sample input:
3
23 13 6
2
23456 56
Sample output:
13236
2345656
Method One
#include <iostream> #include <string> #include <vector> #include <cstdlib> #include < Algorithm>using namespace std;/* * String comparison, the length of the same direct comparison * length is not the same, first compare the length of the same part, if the length of the same part is not the same, then direct comparison can be; * The same length of the same part, and then the remainder of the long string compared with the short string; * In addition, the use of sorting functions need to be noted that there are many places, qsort (str,0,str+n,cmp); very nice usage. And the container is directly with the Begin (), the end () function to replace the design of the *cmp comparison function, note that the return value is an int type * string comparison, with the standard template library inside the function is really very convenient, a variety of functions with good is convenient, with bad, wrong a bunch of */bool CMP ( String str1,string str2) {int flag; BOOL IsBig; int Len1=str1.length (), len2=str2.length (); if (len1==len2) Flag=str1.compare (STR2); else {if (len1<len2) {if (Str2.compare (0,LEN1,STR1) ==0) {isbig=cm P (Str1,str2.substr (LEN1,LEN2-LEN1)); if (IsBig) return true; else return false; } else flag= Str1.compare (STR2); } else {if (Str1.compare (0,LEN2,STR2) ==0) {isbig=cmp (Str1.substr (LEN2,LEN1-LEN2), str2); if (IsBig) return true; else return false; } else flag= Str1.compare (STR2); }} if (flag==1) return false; else return true;} /* General idea: In order to compare the number of inputs in the size of the numbers, first the input number as a string input; * After the input is finished, the string is sorted according to the rules of the topic, detailed sorting rules are described in the comparison function; * Finally, the ordered string output is the required number, and then the output ; *bingo! */int Main () {int n; while (cin>>n) {string str; vector<string>vec; for (int i=0;i<n;i++) {cin>>str; Vec.push_back (str); } sort (Vec.begin (), Vec.end (), CMP); Vector<string>::iterator Iter=vec.begin (); for (; Iter!=vec.end (); iter++) {cout<<*iter; } cout<<endl; } return 0;} /************************************************************** problem:1504 USER:HNDXZTF language:c++ Resu lt:accepted time:170 Ms Memory:1528 kb****************************************************************/
Method Two
#include <iostream> #include <string> #include <vector> #include <algorithm>using namespace std ;/* * First concatenation of the string, and then directly compare the string * After stitching to avoid the occurrence of 321>32>3, so it becomes 332321 */bool cmp (string str1,string str2) {string temp1=str1 , TEMP2=STR2; Temp1.append (STR2); Temp2.append (STR1); return TEMP1<TEMP2;} /* General idea: In order to compare the number of inputs in the size of the numbers, first the input number as a string input; * After the input is finished, the string is sorted according to the rules of the topic, detailed sorting rules are described in the comparison function; * Finally, the ordered string output is the required number, and then the output ; *bingo! */int Main () {int n; while (cin>>n) {string str; vector<string>vec; for (int i=0;i<n;i++) {cin>>str; Vec.push_back (str); } sort (Vec.begin (), Vec.end (), CMP); Vector<string>::iterator Iter=vec.begin (); for (; Iter!=vec.end (); iter++) {cout<<*iter; } cout<<endl; } return 0;} /************************************************************** problem:1504 USER:HNDXZTF language:c++ Result:accepted time:250 Ms memory:1528 kb****************************************************************/
Topic 1504: Nesting arrays into the smallest number