51nod 1097 拼成最小的数 思维

题目:

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1097

题意:

设有n个正整数,将它们联接成一排,组成一个最小的多位整数。

例如:
n=2时,2个整数32,321连接成的最小整数为:32132,
n=4时,4个整数55,31,312, 33 联接成的最小整数为:312313355
Input
第1行:1个数N。(2 <= N <= 10000)
第2 - N + 1行:每行1个正整数。(1 <= A[i] <= 10^9)
Output
输出拼在一起的最小整数。由于数据量太大,请以1000个字符为单位,输出到一行里,最终剩余的不足1000个字符的部分,输出到单独1行。

思路:

按a+b

#include <bits/stdc++.h>
using namespace std;

const int N = 10000 + 10;

string str[N];

bool cmp(string &a, string &b)
{
    string da = a + b, db = b + a;
    return da < db;
}
int main()
{
    ios :: sync_with_stdio(false);
    int n;
    while(cin >> n)
    {
        for(int i = 1; i <= n; i++) cin >> str[i];
        sort(str + 1, str + 1 + n, cmp);
        int tot = 0;
        for(int i = 1; i <= n; i++)
        {
            for(size_t j = 0; j < str[i].size(); j++)
            {
                ++tot;
                if(tot % 1000 == 1 && tot != 1) cout << '\n';
                cout << str[i][j];
            }
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章