Pagini recente » Cod sursa (job #1065396) | Cod sursa (job #1656498) | Cod sursa (job #1121179) | Cod sursa (job #884498) | Cod sursa (job #1878755)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("loto.in");
ofstream fout("loto.out");
vector<unsigned int> lottery;
unsigned int solution[6] = { 0 };
bool found;
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)
{
for (auto it = lottery.begin(); it!=lottery.end(); ++it)
{
solution[k] = *it;
if (k==5)
{
if (checkSolution(sum) && found == false)
{
printSolution();
found = true;
}
}
else
{
backLotto(k + 1,sum);
}
}
}
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);
}
if (maxNum*6<sum)
{
fout << -1;
return 0;
}
backLotto(0,sum);
return 0;
}