The sword refers to the OFFER, which refers to the smallest number of arrays (9 degrees OJ1504)

Source: Internet
Author: User

Description:

Input a positive integer array, splice all the numbers in the array into a number, and print the smallest of all the numbers that can be concatenated. For example, if the input array is {3, 32, 321}, the minimum number that can be arranged for the three numbers is 321323.

 

Input:

The input may contain multiple test examples.
For each test case, the first input behavior is an integer m (1 <= m <= 100) representing the number of input positive integers.
The second line of the input contains m positive integers, each of which cannot exceed 10000000.

 

Output:

Corresponding to each test case,
Output the smallest number that can be formatted with m numbers.

 

Sample input:
323 13 6223456 56

 

Sample output:
132362345656
Solution:

First, the most common idea is to sort the weights at a time to find the smallest number. However, this may time out.

 

Here, weFirst sort the series and then integrate them for the last time.. The algorithm mainly usesBubble SortingTo compare each number with the previous one.

void bubbleSort(char c[][10],int n){    int i,j;    for( i=n-1 ; i>0 ; i-- ){        for(j = n-1;j>(n-1-i);j--){            if(findSmall(c,j))                swap(c,j,j-1);        }    }}

 

 

Use special ideas for comparison ----Concatenate two strings. If String 1 is in front of a small number, put string 1 in front.

int findSmall(char c[][10],int i){    char stri[20];    char strj[20];    strcpy(stri,c[i]);    strcpy(strj,c[i-1]);    strcat(stri,c[i-1]);    strcat(strj,c[i]);    int k;    int length = strlen(stri);     for(k=0;k<length;k++){        if(stri[k] == strj[k])            continue;        else if(stri[k] < strj[k]){            return 1;        }else{            return 0;        }    }}

 

After sorting, you can ensure that the number of directly connected columns is the smallest.

All code:

 

#include <stdio.h>#include <stdlib.h>#include <string.h>void bubbleSort(char c[][10],int n);int findSmall(char c[][10],int i);void swap(char c[][10],int i,int j);int main(){    int n,i;    while(scanf("%d",&n)!=EOF && n>0 && n<=100 ){        int arr[100];        char c[100][10];        char string[1000];        for(i=0;i<n;i++){            scanf("%d",&arr[i]);            sprintf(c[i],"%d",arr[i]);        }        bubbleSort(c,n);        strcpy(string,c[0]);        for(i=1;i<n;i++){            strcat(string,c[i]);        }        printf("%s\n",string);    }    return 0;}void bubbleSort(char c[][10],int n){    int i,j;    for( i=n-1 ; i>0 ; i-- ){        for(j = n-1;j>(n-1-i);j--){            if(findSmall(c,j))                swap(c,j,j-1);        }    }}int findSmall(char c[][10],int i){    char stri[20];    char strj[20];    strcpy(stri,c[i]);    strcpy(strj,c[i-1]);    strcat(stri,c[i-1]);    strcat(strj,c[i]);    int k;    int length = strlen(stri);     for(k=0;k<length;k++){        if(stri[k] == strj[k])            continue;        else if(stri[k] < strj[k]){            return 1;        }else{            return 0;        }    }}void swap(char c[][10],int i,int j){    char tmp[10];    int k;    for(k=0;k<10;k++){        tmp[k] = c[i][k];        c[i][k] = c[j][k];        c[j][k] = tmp[k];    }}/**************************************************************    Problem: 1504    User: xhalo    Language: C    Result: Accepted    Time:320 ms    Memory:916 kb****************************************************************/

 

 

 

Related Article

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.