从头挨个找 24 要几步
同一个数组,改成从头挨个找 24。运行下面这段程序:
#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}, 24) << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)