题解 | #查找#

查找

https://www.nowcoder.com/practice/8e0c0e8c78944847be9bca54b59d713f

#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int main(){
    set<int>s;
    //write your code here......
    int n;
    int m;
    cin>>n;
    cin>>m;
    for(int i=0;i<n;++i){
        int num;
        cin>>num;
        s.insert(num);
    }

    int arr_x[m];
    int arr_out[m];
    for(int i=0;i<m;++i){
        cin>>arr_x[i];
    }
    for(int i=0;i<m;++i){
//        bool find=false;
//        for(set<int>::iterator itr = s.begin();itr != s.end();++itr){
//            if(arr_x[i] < *itr){
//                arr_out[i] = *itr;
//                find = true;
//                break;
//            }
//        }
//        if(!find) arr_out[i] = -1;
        auto itr = s.upper_bound(arr_x[i]);
        if(itr != s.end()){
            arr_out[i] = *itr;
        }else{
            arr_out[i] = -1;
        }
    }
    for(int i=0;i<m;++i){
        cout<<arr_out[i]<<endl;
    }
    return 0;
}



全部评论

相关推荐

今天 15:48
门头沟学院 Java
点赞 评论 收藏
分享
10-27 02:29
已编辑
门头沟学院 嵌入式工程师
牛客72783561...:简历不是这么写的,你这两个项目只说了用到了什么技术,却没说取得了什么成果,在我看来这就是你自己做的一个demo,没有价值。你为什么不写你电赛国二的那个项目?
点赞 评论 收藏
分享
牛客41406533...:回答他在课上学,一辈子待在学校的老教授用三十年前的祖传PPT一字一句的讲解,使用谭浩强红皮书作为教材在devc++里面敲出a+++++a的瞬间爆出114514个编译错误来学这样才显得专业
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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