Foreword:
21. Regardless of whether you can enter the retest or not, record the garbage code written on the road. I originally gnawed on "Algorithm Notes", but I felt too much to do it, so I changed it to the Kingway Computer Test Guide.
Title description:
Input array length n Input array a[1…n] Input search number m Input search number b[1…m] Output YES or NO If there is search, then YES, otherwise NO.
Enter description
There are multiple sets of data entered.
Enter n for each group, then enter n integers, then enter m, and then enter m integers (1<=m, n<=100).
Note: The weight of white mice varies.
Output description:
If output YES in n arrays, output NO otherwise.
answer
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<stdio.h>
using namespace std;
bool binarysearch(vector<int>vi, int tag) {
int left = 0, right = vi.size() - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (vi[mid] == tag)
return true;
else if (vi[mid] < tag)
left = mid + 1;
else
right = mid - 1;
}
return false;
}
int main()
{
int n,temp,tag,time;
bool res;
while (scanf("%d", &n) != EOF) {
vector<int> vi;
for (int i = 0; i < n; i++) {
scanf("%d", &temp);
vi.push_back(temp);
}
sort(vi.begin(), vi.end());
cin >> time;
for (int i = 0; i < time; i++) {
scanf("%d",&tag);
res = binarysearch(vi, tag);
if (res)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}