题目描述
输入
第二行依次输入n个各不相同的有序整数,代表表里的元素;
第三行输入整数t,代表要查询的次数;
第四行依次输入t个整数,代表每次要查询的数值。
输出
示例输入
10 1 22 33 55 63 70 74 79 80 87 4 55 10 2 87
示例输出
4 No Found! No Found! 10
#include <bits/stdc++.h>
using namespace std;
typedef struct
{
int *elem;
int len;
int size;
}List;
void creat(List &l,int n)
{
l.elem=new int[20001];
int i,x;
l.len=0;
for(i=0;i<n;i++)
{
cin>>x;
l.elem[i]=x;
l.len++;
}
}
int order(List &l,int n)
{
int x,mid,low,high;
cin>>x;
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(x>l.elem[mid])
low=mid+1;
else if(x<l.elem[mid])
high=mid-1;
else
return mid;
}
return -1;
}
int main()
{
int x,n,t;
cin>>n;
List l;
creat(l,n);
cin>>t;
while(t--)
{
x=order(l,n);
if(x==-1)
cout<<"No Found!"<<endl;
else
cout<<x+1<<endl;
}
return 0;
}