华为OD机试-快递运输

// Online C++ compiler to run C++ program online
#include 
#include 
#include 
#include 

using namespace std;

int main() {
    string str;
    cin >> str;

    //逗号分割
    vector v;
    str += ",";
    while (str.find(",") != string::npos) {
        int found = str.find(",");
        v.push_back(atoi(str.substr(0, found).c_str()));
        str = str.substr(found + 1);
    }

    int maxWeight;
    cin >> maxWeight;

    //排序
    sort(v.begin(), v.end());

    int res = 0;
    int val = 0;
    for (auto x : v) {
        if ((val + x) > maxWeight)
        {
            break;
        }
        val += x;
        ++res;
    }

    cout << res << endl;

    return 0;
}

你可能感兴趣的:(c++)