题解 | #查找#

查找

http://www.nowcoder.com/practice/d93db01c2ee44e8a9237d63842aca8aa

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    std::ios::sync_with_stdio(false);
    int n,m,left,right,temp;
    while(cin >> n){
        int *num = new int[n];
        for(int i = 0;i < n;++i)
            cin >> num[i];
        cin >> m;
        for(int i = 0;i < m;++i){
            cin >> temp;
            sort(num,num + n);
            left = 0,right = n -1;
            while(left <= right){
                int mid = (left + right) / 2;
                if(num[mid] == temp){
                    cout << "YES" << endl;
                    break;
                }
                else if(num[mid] < temp)
                    left = mid + 1;
                else
                    right = mid - 1;
            }
            if(right < left)
                cout << "NO" << endl;
        }
    }
}
全部评论

相关推荐

尊嘟假嘟点击就送:加v细说,问题很大
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务