题解 | 找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,努力中。。。

全部评论

相关推荐

Java大菜狗:纯纯招黑奴,一天还不到两百那么多要求,还不迟到早退,以为啥啊,给一点工资做一堆活,还以不拖欠员工工资为荣,这是什么值得骄傲的事情吗,纯纯***公司
点赞 评论 收藏
分享
昨天 18:09
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务