-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP2249.cpp
More file actions
49 lines (48 loc) · 782 Bytes
/
P2249.cpp
File metadata and controls
49 lines (48 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// Created by YangYang on 2022/11/13.
//
#include<iostream>
using namespace std;
int n,m,a[1000000];
int brinary(int target)//二分法
{
int left=1,right=n,real=0;
while(left<=right)
{
int mid=(right-left)/2+left;
if(a[mid]<target)
{
left=mid+1;
}
else if(a[mid]>target)
{
right=mid-1;
}
else
{
real=mid;
//继续向左找
right=mid-1;
}
}
if(real!=0)
{
return real;
}
return -1;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=m;i++)
{
int target;
cin>>target;
cout<<brinary(target)<<" ";
}
return 0;
}