[Programming question] arrange the array into the smallest number

Source: Internet
Author: User

68. Arrange the array into the smallest number (array and algorithm ).
Question: enter an array of Positive Integers to join them into a number and output the smallest of all the numbers that can be discharged.
For example, if the input array is {32,321}, the minimum numbers 32132 that can be arranged are output.
Provide an algorithm to solve the problem and prove the algorithm.

 

Idea: First, you must consider overflow. I first wanted to use a string and then changed it to list. The idea is to first put the first number into the list, and then insert the following number into the appropriate position in sequence.

The key issue is how to determine which of the two numbers is in front.

① In the case of 353 and 412, it must be the first digit that is smaller than the first digit.

② Compare the next digit if the number is the same

③ In the case of numbers like 3 and 32, remove the long numbers with the same front and back, and compare them with the short ones.

Such as 3 and 3332

3. 332

3. 32

3. 2. Place the numbers behind them in front.

Another example is 321321321 and 321.

321321, 321

321 and 321 are equal.

 

To obtain each digit, a structure is defined to store the number of each digit and the size of each digit. The overall code is as follows:

/* 68. Arrange the array into the smallest number (array and algorithm ). Question: enter an array of Positive Integers to join them into a number and output the smallest of all the numbers that can be discharged. For example, if the input array is {32,321}, the minimum numbers 32132 that can be arranged are output. Provide an algorithm to solve the problem and prove the algorithm. Start to code = 18: 42end time = 19: 47 */# include <iostream> # include <list> using namespace STD; typedef struct seperatenum {int num [30]; // The Front position is the low position int Len;} seperatenum; seperatenum getseperatenum (int n) {seperatenum s; S. len = 0; int t = n; while (T! = 0) {S. num [S. len ++] = T % 10; t = T/10;} If (S. len = 0) // only one digit 0 {S. num [0] = 0; S. len = 1;} return s;} bool isbefore (seperatenum A, int Na, seperatenum B, int Nb) // whether a should be placed before B. N is the number of the current judgment number {If (B. len-Nb <0 &. len-Na <0) // two numbers are equal, or one number is another number, which is copied n times. 321, which is placed in front of the same {return true;} else if (B. len-Nb <0 &. len-Na> = 0) // The number B is short and the front of B and A are equal {return isbefore (A, Na, B, 1) ;} Else if (B. len-NB> = 0 &. len-Na <0) // A is short. {return isbefore (A, 1, B, Nb);} if (. num [. len-Na]> B. num [B. len-Nb]) {return false;} else if (. num [. len-Na] <B. num [B. len-Nb]) {return true;} else // determine the next digit {return isbefore (A, Na + 1, B, Nb + 1) if the base digit is equal );}} void getminnum (int * In, int Len) // input array and length {list <seperatenum> lminnum; List <seperatenum >:: iterator it; seperatenum temp = Getseperatenum (in [0]); bool isinsert = false; lminnum. push_back (temp); For (INT I = 1; I <Len; I ++) {isinsert = false; temp = getseperatenum (in [I]); for (IT = lminnum. begin (); it! = Lminnum. end (); It ++) {If (isbefore (temp, 1, * It, 1) // you need to insert {lminnum. insert (it, temp); isinsert = true; break;} If (isinsert = false) // No Insert at the end of The Link {lminnum. push_back (temp) ;}}cout <"The minimum combination number is:"; for (IT = lminnum. begin (); it! = Lminnum. End (); It ++) // output the smallest number. Make sure to use '! = 'Cannot be used' <'{for (INT I = it-> len-1; I> = 0; I --) {cout <it-> num [I] ;}} cout <Endl ;}int main () {int A [5] ={ 321,321 321325, 3, 32 }; getminnum (A, 4); Return 0 ;}

I found a good C ++ web site http://www.cplusplus.com/reference/list/list/insert/.

Over 100 lines have been written, and complicated things such as user-defined structures, STL, and Recursion have been used. It took more than an hour, not including the time of thinking.

 

Find answers online and find that answers are concise.

The http://blog.csdn.net/cxllyg/article/details/7659525 has been verified that the answer here is correct and there is proof

The method is quite simple, so I did not think of it. We just need to put both numbers a and B Together to compare the numbers AB and Ba.

Strcmp is used directly in the code.

#include <iostream>#include <string.h>using namespace std;const int g_MaxNumberLength=10;char* g_StrCombine1=new char[g_MaxNumberLength*2+1];char* g_StrCombine2=new char[g_MaxNumberLength*2+1];int compare(const void* strNumber1, const void* strNumber2){    strcpy(g_StrCombine1, *(const char**)strNumber1);    strcat(g_StrCombine1, *(const char**)strNumber2);    strcpy(g_StrCombine2, *(const char**)strNumber2);    strcat(g_StrCombine2, *(const char**)strNumber1);    return strcmp(g_StrCombine1, g_StrCombine2);}void PrintMinNumber(int *numbers, int length){    if(numbers==NULL || length<=0)        return;    char** strNumbers=(char**)(new int[length]);    for(int i=0; i<length; i++)    {        strNumbers[i]=new char[g_MaxNumberLength+1];        sprintf(strNumbers[i], "%d", numbers[i]);    }    qsort(strNumbers, length, sizeof(char*), compare);    for(int i=0; i<length; i++)        cout<<strNumbers[i];    cout<<endl;    for(int i=0; i<length; i++)        delete[] strNumbers[i];    delete[] strNumbers;}void main(){    int Num;    cin>>Num;    int *numbers=new int[Num];    for(int i=0; i<Num; i++)        cin>>numbers[i];    PrintMinNumber(numbers, Num);    getchar();}

 

Another implementation:

#include <iostream>#include <string>#include <sstream>#include <algorithm>using namespace std;bool compare(const string& str1, const string &str2){    string s1=str1+str2;    string s2=str2+str1;    return s1<s2;}void ComArrayMin(int *pArray, int num){    int i;    string *pStrArray=new string[num];    for(i=0; i<num; i++)    {        stringstream stream;        stream<<pArray[i];        stream>>pStrArray[i];            }    sort(pStrArray, pStrArray+num, compare);    for(i=0; i<num; i++)        cout<<pStrArray[i];    cout<<endl;    delete[] pStrArray;}void main(){    int Num;    cin>>Num;    int *pArray=new int[Num];    for(int i=0; i<Num; i++)        cin>>pArray[i];    ComArrayMin(pArray, Num);}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.