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

面包订单

#057小挑战输出vector约 6 分钟

面包店要打一张今天的订单。breads 和 counts 一一对应:第 i 种面包订了 counts[i] 个。在循环里:订了 0 个的跳过;其他的显示一行,比如 croissant x3,再把个数加到 total。最后一行已经写好。

应该显示
croissant x3
baguette x2
muffin x5
total: 10
main.cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    vector<string> breads = {"croissant", "bagel", "baguette", "muffin"};
    vector<int> counts = {3, 0, 2, 5};
    int total = 0;
    for (int i = 0; i < (int)breads.size(); i++) {
        // 在这里写:0 个就跳过;不然显示一行,加到 total
    }
    cout << "total: " << total << endl;
    return 0;
}
Ctrl + Enter 也能运行
结果

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