zoukankan      html  css  js  c++  java
  • 剑指OFFER之把数组排成最小的数(九度OJ1504)

    题目描述:

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

     

    输入:

    输入可能包含多个测试样例。
    对于每个测试案例,输入的第一行为一个整数m (1<=m <=100)代表输入的正整数的个数。
    输入的第二行包括m个正整数,其中每个正整数不超过10000000。

     

    输出:

    对应每个测试案例,
    输出m个数字能排成的最小数字。

     

    样例输入:
    3
    23 13 6
    2
    23456 56
    样例输出:
    13236
    2345656

    解题思路:

      首先,最普通的思路就是权进行一次排列,找出最小的数。但是这样可能会超时。

      这里,我们首先对数列进行排序,最后进行一次整合。算法上面主要采取冒泡排序,对每个数与其前面的数进行比较。

    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);
            }
        }
    }

    在比较时,采用特别的思路----把两个字符串进行拼接,如果字符串1排在前面的数小,那么就把字符串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;
            }
        }
    }

    排序后,可以保证直接进行连接的数列是最小的。

    全部代码:

    #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
    ",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
    ****************************************************************/
  • 相关阅读:
    LeetCode 227. Basic Calculator II
    LeetCode 224. Basic Calculator
    LeetCode 103. Binary Tree Zigzag Level Order Traversal
    LeetCode 102. Binary Tree Level Order Traversal
    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode 169. Majority Element
    LeetCode 145. Binary Tree Postorder Traversal
    LeetCode 94. Binary Tree Inorder Traversal
    LeetCode 144. Binary Tree Preorder Traversal
  • 原文地址:https://www.cnblogs.com/xing901022/p/3794299.html
Copyright © 2011-2022 走看看