Pagini recente » Cod sursa (job #2674835) | Cod sursa (job #2648404) | Cod sursa (job #2462064) | Cod sursa (job #1518461) | Cod sursa (job #2216511)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin ("loto.in");
ofstream fout ("loto.out");
struct loto {
int s, x, y, z;
};
bool wayToSort (loto a, loto b) {
return a.s < b.s;
}
int bin_search(int f, int l, vector<loto> bilet, int x) {
if (f > l)
return -1;
else
{
int m = (f + l) / 2;
if (x == bilet[m].s)
return m;
if (x > bilet[m].s)
return bin_search(m + 1, l, bilet, x);
else
return bin_search(f, m - 1, bilet, x);
}
}
int main()
{
int N, S, i, nr[102], j, k, poz, ok = 1;
vector<loto> bilet;
loto a;
fin >> N >> S;
for (i = 0; i < N ; i++) {
fin >> nr[i];
}
for (i = 0; i < N; i++)
for (j = i; j < N; j++)
for (k = j; k < N; k++) {
a.s = nr[i] + nr[j] + nr[k];
a.x = nr[i];
a.y = nr[j];
a.z = nr[k];
bilet.push_back(a);
}
sort(bilet.begin(), bilet.end(), wayToSort);
for (unsigned int t = 0; t < bilet.size() && ok == 1; t++) {
poz = bin_search(0, bilet.size(), bilet, S - bilet[t].s);
if (poz != -1) {
fout << bilet[t].x << ' '<< bilet[t].y<< ' '<< bilet[t].z << ' ';
fout << bilet[poz].x << ' '<< bilet[poz].y << ' '<< bilet[poz].z << ' ';
ok = 0;
}
}
if (ok == 1)
fout << poz;
fin.close();
fout.close();
}