题解 | 找x map+stl工具
找x
https://www.nowcoder.com/practice/069e2130430c41229ab25e47fa0949a6
#include <iostream>
#include <map>
using namespace std;
int main() {
int n, target;
map<int, int> dataSource;
pair<int, int> temp; // 准备插入的键值对
while (cin >> n) {
dataSource.clear(); // 重置
for (int i = 0; i < n; i++) {
cin >> temp.first;
temp.second = i;
dataSource.insert(temp);
}
cin >> target;
if (dataSource.find(target) == dataSource.end()) {
cout << "-1" << endl; // 未找到
} else {
cout << dataSource[target] << endl; // 返回下标value
}
}
return 0;
}
#考研##复试练习##笔试#2025考研复试 文章被收录于专栏
复试ing,努力中。。。



