Cod sursa(job #1493058)

Utilizator shpincCandrea Laurentiu Vasile shpinc Data 28 septembrie 2015 17:45:00
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;

ifstream f("cautbin.in");
ofstream g("cautbin.out");
int v[100];
int main()
{
    int low,high,mid,x,n,q,pos;
    f>>n>>q;

    for(int i=1;i<=n;i++)
        f>>v[i];
    for(int i=1;i<=q;i++)
    {
        f>>x;
        low=1;
        high=n;
        while(low<=high)
        {
            mid = (low+high)/2;
            if(v[mid]==x)
            {
                pos=mid;

            }
            if(v[mid]>x)
                high=mid-1;
            else
                low=mid+1;
        }
        g<<pos;
    }



    return 0;
}