Cod sursa(job #1051198)

Utilizator silviu12vranau silviu12 Data 9 decembrie 2013 20:08:18
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <iostream>

using namespace std;
int v[1005];
int f(int v[],int n,int x){
    int m,is=0,gasit=-1;
    int iend=n;
        while(is<=iend&&gasit==-1){

            m=(iend-is)/2;
                if(v[m]==x) {gasit=m;}
                else
                    if(x<v[m]){
                        is=0;
                        iend=m-1;

                      }
                    else{
                        is=m+1;
                        iend=n;
                    }

        }

        while(v[gasit+1]==v[gasit]) gasit++;

        return gasit;
}

int main()
{
    int n,x;
    cin>>n>>x;


    for(int i=1;i<=n;i++)cin>>v[i];
    cout<<f(v,n,x);
    return 0;
}