Cod sursa(job #1051365)

Utilizator silviu12vranau silviu12 Data 9 decembrie 2013 22:51:21
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <fstream>
 
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
 
int v[1005];
int cautaBinar(int v[],int n,int x){
    int m,is=1,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]){
                        
                        iend=m-1;
 
                      }
                    else{
                        is=m+1;
                        
                    }
 
        }
        if( gasit != -1) 
        {
            while(v[gasit+1]==v[gasit]) gasit++;
        }
 
        return gasit;
}
 
int main()
{
    int n,x, nrIntrebari,t;
    f>>n ;
 
 
    for(int i=1;i<=n;i++)f>>v[i];
    f >> nrIntrebari;
    f >> t >> x;
    f >> t >> x;
    f >> t >> x;
    g << cautaBinar(v,n,x) << "\n";
    g << x << "\n";
    g << x << "\n";
    return 0;
}