保齡球之夜打了好幾局,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);
}
寫好了先點「執行」試試示例,沒問題再點「提交」。