#include <stdio.h>
#include <algorithm>
using namespace Std;
int a[10000];
int serch (int x,int y,int v)//binary find the lower bound when not found, return is the first subscript greater than V, because when the a[x] is less than V, A[y] is greater than V, mid=x= (y+x)/2, the program will do else, So the return time is the value of Y, pay attention to the bounds;
{
int mid;
while (X<y)
{
Mid= (x+y)/2;
if (A[MID]>=V)
Y=mid;
Else
x=mid+1;
}
return x;
}
int main ()
{
int n,i,m,b;
while (scanf ("%d", &n)!=eof)
{
for (i=0;i<n;i++)
scanf ("%d", &a[i]);
Sort (a,a+n);
scanf ("%d", &m);
for (i=0;i<m;i++)
{
scanf ("%d", &b);
int X=serch (0,N,B);
printf ("%d%d\n", x,a[x]);
}
}
return 0;
}
#include <stdio.h>
#include <algorithm>
using namespace Std;
int a[10000];
int serch (int x,int y,int v)//binary lookup upper bound when not found, returns the first value greater than V, because when A[x] is less than V, A[y] is greater than V, mid=y= (y+x+1)/2, the program executes else, So the return time is the value of Y, pay attention to the bounds;
{
int mid,k=0;
while (X<y)
{
Mid= (x+y+1)/2;//Note this for the median value
if (A[MID]>V)
Y=mid-1;
Else
X=mid;
}
return x;
}
int main ()
{
int n,i,m,b;
while (scanf ("%d", &n)!=eof)
{
for (i=0;i<n;i++)
scanf ("%d", &a[i]);
Sort (a,a+n);
for (i=0;i<n;i++)
printf ("%d", a[i]);
Puts ("");
scanf ("%d", &m);
for (i=0;i<m;i++)
{
scanf ("%d", &b);
int X=serch (0,N,B);
printf ("%d%d\n", x,a[x]);
}
}
return 0;
}
Calling STL
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace Std;
int v[10000];
int main ()
{
int n,m,a,b,i;
while (scanf ("%d%d", &n,&m)!=eof)
{
for (i=0;i<n;i++)
scanf ("%d", &v[i]);
Sort (v,v+n);
for (i=0;i<m;i++)
{
scanf ("%d%d", &a,&b);
Cout<<upper_bound (v,v+n,b) << endl;//returns an upper-bound address
cout <<lower_bound (v,v+n,a) << endl;//return to the Nether address
}
}
return 0;
}
Find the Nether Nether in two points