用数组判重要比多少次
往里加 100 条互不相同的数据,用数组判重。运行下面这段程序,它数的是「比较了多少次」:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
long long list_ops(int n) {
// 数组判重:每加一条,都要和已经在里面的挨个比一次
long long ops = 0;
vector<int> seen;
for (int i = 0; i < n; i++) {
for (int y : seen) {
(void)y;
ops++;
}
seen.push_back(i);
}
return ops;
}
int main() {
cout << list_ops(100) << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)
全部评论