题解 | 找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狠狠赚一...:骗你点star的,港卵公司,记得把star收回去
点赞 评论 收藏
分享
06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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