1 条题解

  • 0
    @ 2024-9-24 21:45:22
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    
    using namespace std;
    
    // 自定义比较函数
    bool compare(const string &a, const string &b) {
        return a + b > b + a; // 按照拼接结果比较
    }
    
    int main() {
        int n;
        cin >> n; // 输入整数的个数
    
        vector<string> nums(n); // 存储输入的数字
        for (int i = 0; i < n; ++i) {
            cin >> nums[i]; // 读取每个数字
        }
    
        // 使用自定义比较函数进行排序
        sort(nums.begin(), nums.end(), compare);
    
        // 拼接所有数字
        string result;
        for (const string &num : nums) {
            result += num;
        }
    
        // 处理前导零的情况
        if (result[0] == '0') {
            cout << "0" << endl; // 如果全是零,输出0
        } else {
            cout << result << endl; // 输出拼接后的结果
        }
    
        return 0;
    }
    
    
    • 1

    信息

    ID
    315
    时间
    1000ms
    内存
    64MiB
    难度
    5
    标签
    递交数
    2
    已通过
    2
    上传者