Interview question selection (74): connect n numbers to obtain the smallest or Largest Integer (Baidu pen)

Source: Internet
Author: User

Description:
There are n positive integers connected into a row to form a minimum integer.
Program input: N count
Program output: Multi-digit join

 

For example:
When n = 2, the minimum integer connected by two integers 32,321 is: 32132,
When N = 4, the minimum integer of the four integers, namely, 55, 31, 312, and 33, is 312313355.

[Question requirements]
1. Just give the pseudo code. Please give the corresponding text description and use the example above to test your algorithm.
2. the time space complexity of the algorithm is given.
3. Prove your algorithm. (Very important)

 

 

Analysis: (The problem is sorting, but the size is not a general comparison)

 

The following is an example of a normal string comparison defect! For example, a = '000000', B = '32', according to the standard string comparison rules, because A> B, so a + B> B +, in fact, '123' <'123 '.
Therefore, we define a comparison rule for a string: If a + B> B + A, we consider A> B. It can be proved that, if a + B> = B + A, B + C> = C + B, there must be: A + C> = C +.
In this way, the program is very simple. In three steps, convert n numbers into strings for storage, and sort n strings according to custom rules; finally, output these strings in ascending order (if the string is from large to small, it is the maximum integer ).
Conclusion: Some problems seem to be mathematical. After careful analysis, we will find that string processing is very simple. In addition, you must master the various functions related to strings, so as not to compile subprograms by yourself.

 

 

Code:

# Include <iostream>
# Include <set>
# Include <string>
Class number
{
Public:
Number (const char * s): STR (s ){}
Bool operator <(const number & target) const
{
If (STR = target. Str)
Return false;
STD: String first = STR + target. STR;
STD: String second = target. Str + STR;
If (first> second)
Return false;
Else if (first <second)
Return true;
Else
Return first. Length () <second. Length ();
};
Const char * getstr () const {return Str. Data ();};
PRIVATE:
STD: String STR;
};
Typedef STD: Set <number> numbers;
Void test (Number * target, int Len)
{
Numbers numstrings;
For (INT I = 0; I <Len; ++ I)
Numstrings. insert (target [I]);
For (numbers: iterator it = numstrings. Begin (); it! = Numstrings. End (); ++ it)
STD: cout <(* It). getstr () <'/T ';
STD: cout <STD: Endl;
}
Int main ()
{
Number all3 [] = {"432", "4321", "43214 "};
Number all [] = {"123", "132", "213", "231", "321", "312 "};
Number all1 [] = {"123123", "12312", "1231", "123", "12", "1 "};
Number all2 [] = {"43214321", "4321432", "432143", "43214", "4321", "432 "};

Number all4 [] = {"12", "3 "};

# Define test (t), sizeof (T)/sizeof (number ))
Test (all3 );
Test (all );
Test (all1 );
Test (all2 );
Test (all4 );
System ("pause ");
Return 0;
}

 

 

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.