Pagini recente » Cod sursa (job #1938114) | Cod sursa (job #3151854) | Cod sursa (job #1438310) | Cod sursa (job #2242578) | Cod sursa (job #1238973)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("loto.in");
ofstream g("loto.out");
int n, S, a[101], i, b[7];
int descresc(int a, int b)
{
return a > b;
}
int cresc(int a, int b)
{
return a < b;
}
int term = 1;
void proc(int k, int Sx, int pos)
{
if (term)
for (int i = k + 1; i <= n; i++)
{
if (a[i] <= Sx)
{
if (b[1] != 0 && b[2] != 0 && b[3] != 0 && b[4] != 0 && b[5] != 0 && b[6] != 0)
{
term = 0;
sort(b + 1, b + 7, cresc);
int v = 0;
for (i = 1; i <= 6; i++) v += b[i];
if (v == S)
{
for (i = 1; i <= 5; i++) g << b[i] << " ";
g << b[6];
}
else g << "-1";
f.close();
g.close();
exit(0);
break;
}
if (Sx - a[i] >= 0)
{
b[pos] = a[i];
proc(i - 1, Sx - a[i], pos + 1);
}
else
{
proc(i, Sx - a[i], pos + 1);
}
}
}
}
int main()
{
f >> n >> S;
for (i = 1; i <= n; i++) f >> a[i];
sort(a + 1, a + n + 1, descresc);
if (n == 1 && S % a[1] == 0) for (int t = 1; t <= 6; t++) g << a[1] << " ";
proc(0, S, 1);
for (int t = 0; t < n; t++)
{
proc(n, S, 1);
for (int l = 1; l <= 6; l++) b[i] = 0;
}
return 0;
}