码迷,mamicode.com
首页 > 编程语言 > 详细

剑指offer系列源码-把数组排成最小的数

时间:2014-12-10 14:18:21      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:blog   io   ar   os   sp   for   on   2014   log   

题目1504:把数组排成最小的数
时间限制:1 秒内存限制:128 兆特殊判题:否提交:1463解决:448
题目描述:
输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
输入:
输入可能包含多个测试样例。
对于每个测试案例,输入的第一行为一个整数m (1<=m <=100)代表输入的正整数的个数。
输入的第二行包括m个正整数,其中每个正整数不超过10000000。
输出:
对应每个测试案例,
输出m个数字能排成的最小数字。
样例输入:
3
23 13 6
2
23456 56
样例输出:
13236
2345656

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
 
int numbers[200];
string str_num[200];
 
bool cmp(const string &str1, const string &str2) {
    string _str1 = str1;
    string _str2 = str2;
    _str1.append(str2);
    _str2.append(str1);
 
    return _str1 < _str2;
 
}
 
int main() {
    int n;
    while(scanf("%d", &n) != EOF) {
        for(int i = 0; i < n; i ++)
            scanf("%d", numbers+i);
 
        for(int i = 0; i < n; i ++) {
            char str[20];
            sprintf(str, "%d", numbers[i]);
            str_num[i] = str;
        }
 
        sort(str_num, str_num+n, cmp);
 
        for(int i = 0; i < n; i ++) {
            cout << str_num[i];
        }
        cout << endl;
    }
    return 0;
}

剑指offer系列源码-把数组排成最小的数

标签:blog   io   ar   os   sp   for   on   2014   log   

原文地址:http://blog.csdn.net/hackcoder/article/details/41844661

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!