Sunteți pe pagina 1din 4

Tema: #4248 - pbinfo

____________________________________________________________________________
#include <iostream>
using namespace std;
int main()
{
int v[100], i, n, x, st, dr, mij, gasit;
cin >> n >> x;
for(i = 1; i <= n; i++)
cin >> v[i];
st = 1;
dr = n;
gasit = 0;
while (st <= dr && gasit == 0)
{
mij = (st + dr) / 2; //determina mijlocul vectorului
if (x > v[mij]) st = mij + 1;
else if (x < v[mij])dr = mij - 1;
else gasit = mij;

} // verifica daca elementul este mai mare sau mai mic decat
//elem din mijloc; dacă e mai mare decât acea valoare, algoritmul se reia, de la mijlocul
vectorului până la sfârșit, iar
//dacă e mai mică, algoritmul se reia pentru elementele de la începutul vectorului până la mijloc.

if(gasit != 0) cout << gasit;


else cout << "NU exista";
return 0;}
____________________________________________________________________________
Ex 1:
a)
#include <iostream>
using namespace std;
int main()
{
int n, i, mij, st, dr, gasit;
float s = 0, ma = 0;
cin >> n; float v[n + 1];
for (i = 1; i <= n; i++)
{
cin >> v[i];
s += v[i];
}
ma = s / n;
st = 1;
dr = n;
gasit = 0;
gasit = 0;
while (st <= dr && gasit == 0)
{
mij = (st + dr) / 2;
if (ma > v[mij]) st = mij + 1;
else if (ma < v[mij]) dr = mij - 1;
else gasit = mij;
}
if (gasit != 0) cout << "Da";
else cout << "Nu";
return 0;
}

b)

#include <iostream>

using namespace std;

int main()
{
int n, i, gasit = 0, st, dr, mij;
float x;
cin >> n >> x;
float v[n + 1];
for(i = 1; i <= n; i++)
cin >> v[i];
st = 1;
dr = n;
while (st <= dr && gasit == 0)
{
mij = (st + dr) / 2;
if (x > v[mij]) st = mij + 1;
else if (x < v[mij]) dr = mij - 1;
else gasit = mij;

}
if (gasit != 0 && gasit % 2 == 0) cout << "Da";
else if (gasit != 0 && gasit % 2 != 0) cout << "Nu";
else cout << "Nu exista";
return 0;
}

#4248 - pbinfo
#include <iostream>

using namespace std;

int main()
{
int n, r, i, j, st, dr, mij, gasit;
Bool ok = true;
cin >> n; int v[n + 1];
for (i = 1; i <= n; i++)
{
cin >> v[i];
if (v[i - 1] == v[i])
{
i--;
n--;
}
cin >> r; int w[r + 1];
for (i = r; i > 1; i--)
{
cin >> w[i];
if (w[i] == w[i - 1])
{
i--;
r--;
}
}

st = 1;
dr = n;
gasit = 0;
while (st <= dr)
{

}
for (i = 1; i <= n; i++)
cout << v[i] << " ";
return 0;
}

S-ar putea să vă placă și