Cod sursa(job #1589527)

Utilizator Vele_GeorgeVele George Vele_George Data 4 februarie 2016 09:19:51
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <iostream>
#include <fstream>
#define nmax 100003
using namespace std;

int n, m, V[nmax], act, x;

int cb_0(int x)
{
    int st=0, dr=n+1,mid;
    while (dr-st!=1)
    {
        mid = (dr+st)/2;
        if (V[mid] <= x) st = mid;
                    else dr = mid;
    }
    if (V[st] == x) return st;
    return -1;
}

int cb_1(int x)
{
    int st=0, dr=n+1,mid;
    while (dr-st!=1)
    {
        mid = (dr+st)/2;
        if (V[mid] <= x) st = mid;
                    else dr = mid;
    }
    return st;
}

int cb_2(int x)
{
    int st=0, dr=n+1,mid;
    while (dr-st!=1)
    {
        mid = (dr+st)/2;
        if (V[mid] < x) st = mid;
                    else dr = mid;
    }
     return dr;
}

int main()
{
    ifstream f("cautbin.in");
    ofstream g("cautbin.out");

    f >> n;
    for(int i=1; i<=n; i++)
    {
        f >> V[i];
    }
    f >> m;
    for(int i=1; i<=m; i++)
    {
        f >> act >> x;
        if (act == 0) g << cb_0(x) << "\n";
        else
        if (act == 1) g << cb_1(x) << "\n";
                 else g << cb_2(x) << "\n";
    }

    f.close();
    g.close();
    return 0;
}