码迷,mamicode.com
首页 > 编程语言 > 详细

把数组排成最小的数/1038. Recover the Smallest Number

时间:2016-01-25 23:00:14      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:

题目描述

输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
 

Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given {32, 321, 3214, 0229, 87}, 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 one test case. Each case gives a positive integer N (<=10000) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.

Output Specification:

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

Sample Input:
5 32 321 3214 0229 87
Sample Output:
22932132143287

 1 class Solution
 2 {
 3     public:        
 4         string PrintMinNumber(vector<int> num)
 5         {
 6             string ans = "";
 7             int len = num.size();
 8             if(len==0)
 9                 return ans;
10             vector<string> nums(len);
11             for(int i = 0; i<len; i++)
12                 nums[i] = to_string(num[i]);
13             sort(nums.begin(),nums.end(),cmp);
14             for(int i = 0; i<len; i++)
15                 ans+=nums[i];
16             return ans;
17         }
18         static int cmp(const string &s1,const string &s2)
19         {
20             return s1+s2<s2+s1;
21         }
22 };

 

把数组排成最小的数/1038. Recover the Smallest Number

标签:

原文地址:http://www.cnblogs.com/xiaoyesoso/p/5158892.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!