北辰最后坐在哪一格
同样的四个人、同样的顺序。运行下面这段程序:
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// 开放寻址:一格只坐一个学号,-1 表示空格
int index_of(const vector<int>& t, int key) {
for (size_t i = 0; i < t.size(); i++) {
if (t[i] == key) return (int)i;
}
return -1;
}
int main() {
vector<int> table(8, -1);
for (int key : {17, 24, 15, 23}) {
int i = key % 8;
while (table[i] != -1) i = (i + 1) % 8;
table[i] = key;
}
cout << index_of(table, 23) << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)
全部评论