1 条题解

  • 0
    @ 2026-9-30 12:58:25

    C++ :

    #include <cstdio>
    #include <string>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    /*
      最大拼接数:把 n 个正整数按某种顺序拼成一个尽可能大的整数。
      自定义比较:a 排在 b 前面 <=> a+b > b+a(拼接后的字典序更大)。
      该比较满足传递性,排序后依次输出即为最大拼接结果。
      n <= 100、每个数 <= 10^9,答案最长可达 1000 位,必须按字符串处理与输出。
      样例 3 / 13 312 343 -> 34331213。
    */
    static string s[105];
    bool cmp(const string &a, const string &b){ return a + b > b + a; }
    int main(){
        int n;
        if(!(cin >> n)) return 0;
        for(int i = 0; i < n; i++) cin >> s[i];
        sort(s, s + n, cmp);
        for(int i = 0; i < n; i++) cout << s[i];
        cout << endl;
        return 0;
    }
    
    
    • 1

    信息

    ID
    2846
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者