Find the Nether Nether in two points

Source: Internet
Author: User

#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]&GT;=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]&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.