Kodewick
开始学 →
基础←→0 / 60

保龄球之夜

#037基础引用vector约 5 分钟

保龄球之夜打了好几局,scores 是每局的分数,至少一局。low_high 用两个引用参数 low、high 带回最低分和最高分。写循环里面:比 low 低就换掉 low,比 high 高就换掉 high。

示例
score_range({120, 98, 145, 110})→'98 ~ 145'
score_range({77})→'77 ~ 77'
函数string score_range(const vector<int> &scores)
main.cpp
#include <string>
#include <vector>
using namespace std;

void low_high(const vector<int> &scores, int &low, int &high) {
    low = scores[0];
    high = scores[0];
    for (int s : scores) {
        // 在这里写:更低就换 low,更高就换 high
    }
}

string score_range(const vector<int> &scores) {
    int low = 0, high = 0;
    low_high(scores, low, high);
    return to_string(low) + " ~ " + to_string(high);
}
Ctrl + Enter 也能运行
结果

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