Typical questions and answers for the test interview-Continuous updates

Source: Internet
Author: User

Interview Questions (1 ):Connect n numbers to obtain the smallest or Largest Integer (Baidu exam)

 

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. Learn how to use basic string functions and set functions ......

 

Source code:

# Include <iostream>
# Include <set>
# Include <string>
Using namespace STD;

Class number
{
Public:
Number (const char * s): STR (s ){}
// Use the set. For the number of the custom type, the '<' operator must be overloaded.
Bool operator <(const number & target) const
{
If (STR = target. Str)
Return false; // set does not contain repeated Elements
String first = STR + target. STR;
String second = target. Str + STR;
If (first> second)
Return false;
Else if (first <second)
Return true;
Else // I don't know why this logic should not exist. The two must have the same length.
Return first. Length () <second. Length ()? True: false;
};
Const char * getstr () const
{
Return Str. c_str (); // or return Str. Data ();
};
PRIVATE:
STD: String STR;
};

Typedef STD: Set <number> numbers;
Void test (Number * target, int Len)
{
Numbers numstrings;
// Insert data to the set in ascending order
For (INT I = 0; I <Len; ++ I)
Numstrings. insert (target [I]);
// Output in order to form a minimum number of Multiple Digits. If output in reverse order, output the maximum number of digits.
For (numbers: iterator it = numstrings. Begin (); it! = Numstrings. End (); ++ it)
STD: cout <(* It). getstr () <"";
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 "};

// Macro done is a processing function. sizeof calculates the length based on the Type alignment principle.
# Define done (t) test (t), sizeof (T)/sizeof (number ))

Done (all3 );
Done (all );
Done (all1 );
Done (all2 );
Done (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.