运气最好时要几步
在 [13, 15, 17, 23, 24] 里从头挨个找 13。运行下面这段程序:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int lsearch_steps(const vector<int>& a, int target) {
int steps = 0;
for (int x : a) {
steps++;
if (x == target) return steps;
}
return steps;
}
int main() {
cout << lsearch_steps({13, 15, 17, 23, 24}, 13) << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)