Kodewick
开始学 →
小挑战←→0 / 60

今天卖得最好

#031小挑战map循环约 7 分钟

奶茶店每卖一杯就记下名字。写 best_seller(sales),返回卖得最多的那种。一样多的,返回字母顺序在前的。至少卖出了一杯。

示例
best_seller({"latte", "tea", "latte"})→'latte'
best_seller({"mocha"})→'mocha'
函数string best_seller(const vector<string> &sales)
main.cpp
#include <map>
#include <string>
#include <vector>
using namespace std;

string best_seller(const vector<string> &sales) {
    map<string, int> count;
    for (const string &drink : sales) {
        // 在这里写:这种饮料的杯数加 1
    }
    string best = "";
    int most = 0;
    for (const auto &item : count) {
        // 在这里写:比 most 多,就记下这一种和它的杯数
    }
    return best;
}
Ctrl + Enter 也能运行
结果

写好了先点「运行」试试示例,没问题再点「提交」。