Mai intai trebuie sa te autentifici.
Cod sursa(job #2777089)
| Utilizator | Data | 22 septembrie 2021 09:23:27 | |
|---|---|---|---|
| Problema | Cautare binara | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 1.34 kb |
#include <iostream>
#include <fstream>
using namespace std;
const int NMAX=100000;
int n,m,v[NMAX+1];
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int caut0(int x){
int p,u,poz;
p=1;
u=n;
poz=-1;
while (p<=u){
m=(p+u)/2;
if (x==v[m]){
poz=m;
}
if (x>=v[m]){
p=m+1;
}
else {
u=m-1;
}
}
return poz;
}
int caut1(int x){
int p,u,poz;
p=1;
u=n;
poz=-1;
while (p<=u){
m=(p+u)/2;
if (x>=v[m]){
poz=m;
p=m+1;
}
else {
u=m-1;
}
}
return poz;
}
int caut2(int x){
int p,u,poz;
p=1;
u=n;
poz=-1;
while (p<=u){
m=(p+u)/2;
if (x<=v[m]){
u=m-1;
}
else {
p=m+1;
}
}
return poz;
}
int main()
{
int m,x,t;
f>>n;
for (int i=1;i<=n;i++){
f>>v[i];
}
f>>m;
while (m--){
f>>t>>x;
switch(t){
case 0:
g<<caut0(x)<<"\n";
break;
case 1:
g<<caut1(x)<<"\n";
break;
case 2:
g<<caut2(x)<<"\n";
}
}
return 0;
}
