PAT 1038. Recover the smallest number (30)

Source: Internet
Author: User

1038. Recover the smallest number (30)

Given a collection of number segments, you is supposed to recover the smallest number from them. For example, given {321, 3214, 0229,}, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321- 3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.

Input Specification:

Each input file contains the one test case. Each case gives a positive an integer n (<=10000) followed by N number segments. Each segment contains a non-negative an integer of no more than 8 digits. All the numbers in a line is separated by a space.

Output Specification:

For each test case, print the smallest number on one line. Do not output leading zeros.

Sample Input:
5 32 321 3214 0229 87
Sample Output:
22932132143287
1#include <iostream>2#include <string>3#include <vector>4#include <algorithm>5 6 using namespacestd;7 8 BOOLcmpConst string& S1,Const string&S2)9 {Ten     returnS1 + s2 < s2 +S1; One } A  - intMain () - { the     intSegnum; -vector<string>segment; -CIN >>Segnum; -      for(inti =0; i < Segnum; i++) +     { -         stringseg; +CIN >>seg; A segment.push_back (SEG); at     } - sort (Segment.begin (), Segment.end (), CMP); -     stringRes; -      for(inti =0; I < segment.size (); i++) -Res + =Segment[i]; -  in     string:: Iterator it =Res.begin (); -      while(*it = ='0') toit =Res.erase (it); +  -     if(Res.empty ()) thecout <<"0"; *     Else $cout <<Res;Panax Notoginseng}

PAT 1038. Recover the smallest number (30)

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.