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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章