How to arrange the smallest number of arrays

Source: Internet
Author: User
Tags arrays include integer printf sprintf

Topic Description:

Enter a positive integer array that combines all the numbers in the array into a number, printing the smallest of all the numbers that can be spliced. For example, enter the array {3,32,321}, and print out the smallest number that the three numbers can line up to be 321323.

Input:

The input may contain more than one test sample.

For each test case, the first behavior of the input an integer m (1<=m <=100) represents the number of positive integers entered.

The second line you enter includes m positive integers, where each positive integer does not exceed 10000000.

Output:

corresponding to each test case,

The smallest number that can be arranged by the output m number.

Sample input: 323 13 6223456 56 Sample output: 132362345656 The main problem is to customize a rule that compares the size of a combined number, primarily for numbers m and N, first converting it to a string, and then comparing its combined MN and NM size, This is compared directly to the size standard of the string, and finally, the qsort is used to sort the string in the array of strings from left to right, even if the smallest string is converted to the smallest integer.

The AC code is as follows:

#include <stdio.h> #include <stdlib.h> #include <string.h> char strs[100][10]; /* Custom comparison rules return to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg//int mycompare (const void *STR1,  
    const void *STR2) {static char s1[20];  
    static Char s2[20];  
    Char *string1 = (char *) str1;  
    Char *string2 = (char *) str2;  
    Merge two strings together sprintf (S1, "%s%s", string1,string2);  
      
    sprintf (S2, "%s%s", string2,string1);  
Return strcmp (S1,S2); /* * The smallest integer is printed as a string/void Printminnum (int *nums,int len) {if (Nums==null | | len<1) retur  
      
    N  
    int i;  
      
    Writes an integer to the string for (i=0;i<len;i++) sprintf (Strs[i], "%d", nums[i]);  
          
    Sorts Qsort (strs,len,10*sizeof (char), mycompare) according to the rules specified in Mycompare;  
    for (i=0;i<len;i++) printf ("%s", Strs[i]);  
printf ("\ n");  
    int main () {int nums[100];  
 int m;   while (scanf ("%d", &m)!= EOF) {int i;  
      
        for (i=0;i<m;i++) scanf ("%d", nums+i);  
    Printminnum (NUMS,M);  
return 0; }

/**************************************************************

problem:1504

User:mmc_maodun

Language:c++

result:accepted

time:240 ms

memory:1020 kb

****************************************************************/

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.