18963 最大数字

这个问题可以使用贪心算法来解决。我们需要定义一个新的比较函数,对于两个数字a和b,如果ab > ba,那么我们就说a大于b。然后我们将所有的数字按照这个新的比较函数进行排序,然后将排序后的数字拼接起来,就可以得到最大的数字。

以下是C++代码实现:

#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 auto& num : nums) {
        result += num;
    }

    cout << result << endl;

    return 0;
}

在这段代码中,我们首先读取输入的N和所有的数字,然后将所有的数字按照新的比较函数进行排序。然后我们遍历所有的数字,将它们拼接起来,就可以得到最大的数字。最后,我们输出这个最大的数字。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值