Pagini recente » Cod sursa (job #2106992) | Cod sursa (job #1242066) | Cod sursa (job #1271938) | Cod sursa (job #1122803) | Cod sursa (job #2216514)
#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;
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++) {
bilet.push_back({nr[i] + nr[j] + nr[k], nr[i], nr[j], nr[k]});
}
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();
}