Reprint Please specify the Source: http://blog.csdn.net/ns_code/article/details/28128551
-
title Description:
-
Enter an array of positive integers. Combine all the numbers in the array into a number, and print the smallest of all the numbers that can be stitched together. For example, input array {3, 32. 321}, the smallest number that can be printed out of these three digits is 321323.
-
input:
-
The input may include multiple test examples.
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 of
input includes m positive integers, each of which does not exceed 10000000.
-
Output:
-
Each test case,
The smallest number that the output m number can be.
-
Example input:
-
323 13 6223456 56
-
Example output:
-
132362345656
The main problem is to define a combination of the number of the size of the rule, mainly for the number M and N. Turn it into a string first. And then the size of the combination of MN and NM, here directly according to the size of the string standard to compare, and finally sorted by Qsort, the string array of sorted strings from left to right combination of strings, even the smallest string. The smallest integer that is converted to an integer.
The AC code is as follows:
#include <stdio.h> #include <stdlib.h> #include <string.h>char strs[100][10];/* their own definition of comparative rules */int Mycompare (const void *str1,const void *STR2) {static char s1[20];static char S2[20];char *string1 = (char *) Str1;char *stri NG2 = (char *) str2;//merges two strings together sprintf (S1, "%s%s", String1,string2), sprintf (S2, "%s%s", string2,string1); return strcmp (S1,S2);} /* Prints the smallest integer as a string */void printminnum (int *nums,int len) {if (Nums==null | | len<1) return;int i;//writes an integer to the string for (i=0;i <len;i++) sprintf (Strs[i], "%d", nums[i]);//Sort Mycompare (Qsort (char), strs,len,10*sizeof) according to the rules specified in Mycompare; for (i =0;i<len;i++) printf ("%s", Strs[i]);p rintf ("\ 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;}
/**************************************************************
&NBSP;&NBSP;&NBSP;&NBSP;
problem:1504
&NBSP;&NBSP;&NBSP;&NBSP;
user:mmc_maodun
&NBSP;&NBSP;&NBSP;&NBSP;
language:c++
&NBSP;&NBSP;&NBSP;&NBSP;
result:accepted
&NBSP;&NBSP;&NBSP;&NBSP;
time:240 Ms
&NBSP;&NBSP;&NBSP;&NBSP;
memory:1020 KB
****************************************************************/
"Point of Offer" the smallest number of arrays