保龄球之夜打了好几局,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)#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);
}
写好了先点「运行」试试示例,没问题再点「提交」。