Pagini recente » Cod sursa (job #357451) | Cod sursa (job #1667541) | Cod sursa (job #2538182) | Cod sursa (job #2894819) | Cod sursa (job #1878837)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("loto.in");
ofstream fout("loto.out");
vector<unsigned int> lottery;
unsigned int solution[6] = { 0 };
bool found;
unsigned int partialSum = 0;
void printSolution()
{
for (unsigned int i = 0; i < 6; ++i)
{
fout << solution[i] << ' ';
}
}
bool checkSolution(unsigned int sum)
{
unsigned int sumSol = 0;
for (unsigned int i = 0; i < 6; ++i)
{
sumSol += solution[i];
}
if (sumSol == sum) { return true; }
else return false;
}
void backLotto(unsigned int k, unsigned int sum, bool ok)
{
for (auto it = lottery.begin(); it != lottery.end(); ++it)
{
if (((partialSum + (*it)) <= sum) || it == lottery.end() - 1)
{
solution[k] = *it;
ok = true;
}
if (k == 5 && ok)
{
if (checkSolution(sum) && found == false)
{
printSolution();
found = true;
}
}
else
{
if (ok)
{
backLotto(k + 1, sum, false);
}
}
}
}
int main()
{
unsigned int lotteryNumbers = 0, sum = 0, maxNum = 0;
fin >> lotteryNumbers >> sum;
for (unsigned int i = 0; i < lotteryNumbers; i++)
{
unsigned int x;
fin >> x;
if (x > maxNum)
{
maxNum = x;
}
lottery.push_back(x);
}
sort(lottery.begin(), lottery.end(), [](int a, int b) {if (a > b)return true; else return false; });
if (maxNum * 6 < sum)
{
fout << -1;
return 0;
}
backLotto(0, sum, false);
return 0;
}