老師改小測驗,answers 裡 true 是答對,false 是答錯。mark 用兩個引用引數 right、wrong 帶回對了幾題、錯了幾題。寫迴圈裡面。沒有題目時兩個都是 0。
quiz_result({true, true, false, true})→'3 right, 1 wrong'quiz_result({})→'0 right, 0 wrong'string quiz_result(const vector<bool> &answers)#include <string>
#include <vector>
using namespace std;
void mark(const vector<bool> &answers, int &right, int &wrong) {
right = 0;
wrong = 0;
for (bool ok : answers) {
// 在這裡寫:對了 right 加 1,錯了 wrong 加 1
}
}
string quiz_result(const vector<bool> &answers) {
int right = 0, wrong = 0;
mark(answers, right, wrong);
return to_string(right) + " right, " + to_string(wrong) + " wrong";
}
寫好了先點「執行」試試示例,沒問題再點「提交」。