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


    题目描述

    输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{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 };
  • 相关阅读:
    Delphi防止同时出现多个应用程序实例CreateMutex
    DLL注入代码
    DLL注入代码
    C语言学习笔记
    随笔
    存储器简介
    随笔
    对偶问题的基本性质
    C语言学习笔记
    对偶问题的基本性质
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/5158892.html
Copyright © 2020-2023  润新知