Cod sursa(job #2266493)

Utilizator gheorghe-eduGheorghe Eduard gheorghe-edu Data 22 octombrie 2018 18:37:09
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <iostream>
#include <fstream>

using namespace std;

int v[100001],n,m;
const int c=16;
ofstream out ("cautbin.out");
 ifstream in ("cautbin.in");
void citire()
{
    in>>n;
    for(int i=1; i<=n; i++)
        in>>v[i];
    in>>m;
}
int caut0 (int x)
{
    int r=0,pas=1<<c;
    while(pas!=0)
    {
        if(r+pas<=11&&v[r+pas]<=x)
        {
            r+=pas;
        }
        pas/=2;
    }
    if(v[r]!=x)
    {
        out<< -1;
    }
    out<<r;
}

int caut1 (int x)
{
    int r=0,pas=1<<c;
    while(pas!=0)
    {
        if(r+pas<=11&&v[r+pas]<=x)
        {
            r+=pas;
        }
        pas/=2;
    }
    out<< r;
}

int caut2 (int x)
{
    int r=0,pas=1<<c;
    while(pas!=0)
    {
        if(r+pas<=11&&v[r+pas]<x)
        {
            r+=pas;
        }
        pas/=2;
    }
    r++;
    out<< r;
}

int main()
{
    citire();
    int t,x;
    for(int i=0; i<m; i++)
    {
        in>>t>>x;
        if(t==0)caut0 (x);
        if(t==1)caut1 (x);
        if(t==2)caut2 (x);
    }

    return 0;
}