Pagini recente » Cod sursa (job #1403884) | Cod sursa (job #602200) | Cod sursa (job #137354) | Cod sursa (job #59786) | Cod sursa (job #1041554)
#include<iostream>
#include<fstream>
using namespace std;
ifstream f ("cautbin.in");
ofstream g ("cautbin.out");
int *a, n, m, val;
short code;
int search0 (int val)
{
int step, i;
for (step = 1; step < n; step <<= 1);
for (i = 0; step; step >>= 1)
if (i + step < n && a[i + step] <= val)
i += step;
if (a[i] == val)
return i;
else
return -2;
}
int search1 (int val)
{
int step, i;
for (step = 1; step < n; step <<= 1);
for (i = 0; step; step >>= 1)
if (i + step < n && a[i + step] <= val)
i += step;
return i;
}
int search2 (int val)
{
int step, i;
for (step = 1; step < n; step <<= 1);
for (i = 0; step; step >>= 1)
if (i + step < n && a[i + step] < val)
i += step;
if (i + 1 == n || a[i] == val)
return i;
else
return i + 1;
}
int main ()
{
f >> n;
a = new int[n];
for (int i = 0; i < n; i++)
f >> a[i];
f >> m;
for (int i = 1; i <= m; i++)
{
f >> code >> val;
switch(code)
{
case 0:
g << search0(val) + 1 << endl;
break;
case 1:
g << search1(val) + 1 << endl;
break;
case 2:
g << search2(val) + 1 << endl;
}
}
return 0;
}