Cod sursa(job #1516236)

Utilizator enouGhAbu Ras Mohamed Ata Radu enouGh Data 2 noiembrie 2015 21:15:55
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.01 kb
#include<fstream>
using namespace std;
int v[100001];
int n,poz,s,nrintrebari,i,tip,x;
int cb0(int x){
                int s=1 , d=n , poz=-1 ;
                int m;
                while (s<=d)
                {
                    m=(s+d)/2;
                    if (x==v[m])
                    {
                        poz=m;
                        s=m+1;

                    }
                    else
                        if(x<v[m])
                            d=m-1;
                        else
                            s=m+1;
                }
                return poz;
              }
int cb1(int x){
                int s=1 , d=n , poz=-1 ,m;
                while (s<=d)
                {
                    m=(s+d)/2;
                    if (x>=v[m])
                    {
                        poz=m;
                        s=m+1;

                    }
                    else
                        d=m-1;


                }
               return poz;
              }

 int cb2(int x){
                int s=1 , d=n , poz=-1 ,m;
                while (s<=d)
                {
                    m=(s+d)/2;
                    if (x<=v[m])
                    {
                        poz=m;
                        d=m-1;

                    }
                    else
                        s=m+1;


                }
                return poz;
              }

int main(){
            ifstream in("cautbin.in");
            ofstream out("cautbin.out");
            in>>n;
            for(i=1;i<=n;i++)
                in>>v[i];
            in>>nrintrebari;
            for(i=1;i<=nrintrebari;i++)
            {
                in>>tip;
                in>>x;
                if(tip==0)
                    out<<cb0(x)<<'\n';
                if(tip==1)
                    out<<cb1(x)<<'\n';
                if(tip==2)
                    out<<cb2(x)<<'\n';

            }
            in.close();
            out.close();
return 0;
}