Pagini recente » Cod sursa (job #421747) | Cod sursa (job #1435216) | Cod sursa (job #2220285) | Cod sursa (job #180941) | Cod sursa (job #499521)
Cod sursa(job #499521)
#include<iostream.h>
#include<fstream.h>
using namespace std;
unsigned t,x[100];
int caut1(int sfarsit, int lungime)
{
int m;
if(sfarsit>lungime) return -1;
else
{
m=(sfarsit+lungime)/2;
if(t==x[m])
{
while(t==x[m])
{
m++;
}
return m-1;
}
if(t<x[m]) return caut1(sfarsit,m-1);
if (t>x[m]) return caut1(m+1,lungime);
}
}
int caut(int sfarsit, int lungime)
{
int m;
if(sfarsit>lungime) return -1;
else
{
m=(sfarsit+lungime)/2;
if(t==x[m])
{
while(t<=x[m])
{
m--;
}
return m+1;
}
if(t<x[m]) return caut(sfarsit,m-1);
if (t>x[m]) return caut(m+1,lungime);
}
}
int main()
{
int n,i,p,k,m,ok;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
f>>n;
for(i=1;i<=n;i++)
f>>x[i];
f>>m;
k=1;
while(k<=m)
{
f>>p>>t;
if(p==0)
{
g<<caut1(1,t);
g<<endl;
}
if(p==1)
{
ok=0;
while(ok==0)
{
if(caut1(1,t)==-1)
t--;
else ok=1;
}
g<<caut1(1,t);
g<<endl;
}
if(p==2)
{
g<<caut(1,t);
g<<endl;
}
k++;
}
}