Cod sursa(job #2844791)

Utilizator DanielRusuDaniel Rusu DanielRusu Data 5 februarie 2022 14:32:09
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
#include <iostream>
using namespace std;
int n,i,dr,st,mij,x,v[101],m,j,c;
int main()
{
    cin>>n;
    for(i=1; i<=n; i++)
    {
        cin>>v[i];
    }
    cin>>m;
    for(j=1;  j<=m; j++)
    {
        cin>>c>>x;
        if(c==0)
        {
            st=1;
            dr=n;
            while(st<=dr)
            {
                mij=(st+dr)/2;
                if(v[mij]>x)
                {
                    dr=mij-1;
                }
                else
                {
                    st=mij+1;
                }
            }
            if(v[dr]==x)
            {
                cout<<dr<<endl;
            }
            else
            {
                cout<<-1<<endl;
            }
        }
        if(c==1)
        {
            st=1;
            dr=n;
            while(st<=dr)
            {
                mij=(st+dr)/2;
                if(v[mij]>x)
                {
                    dr=mij-1;
                }
                else
                {
                    st=mij+1;
                }
            }
            cout<<dr<<endl;
        }
        if(c==2)
        {
            st=1;
            dr=n;
            while(st<=dr)
            {
                mij=(st+dr)/2;
                if(v[mij]>=x)
                {
                    dr=mij-1;
                }
                else
                {
                    st=mij+1;
                }
            }
            cout<<st<<endl;
        }

    }
    return 0;
}