Pagini recente » Cod sursa (job #3290387) | Cod sursa (job #2032398) | Cod sursa (job #1679123) | Cod sursa (job #2174380) | Cod sursa (job #1067252)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdlib>
#include <vector>
using namespace std;
vector<int> numbers;
int n, s, v[10];
int it = 0;
void write_sol_and_quit() {
ofstream out("loto.out");
for (int i = 1; i <= 6; ++i) {
out << (i == 1 ? "" : " ") << v[i];
}
out << std::endl;
out.close();
exit(0);
}
void gen(int current_level, int sofar, int start, int end) {
it++;
for (int i = start; i <= end; ++i) {
// Will we underrun with the current choice?
if (sofar + numbers[i] + (6 - current_level) * numbers[end] < s) {
continue;
}
// Will we overrun with the current choice? Stop and backtrack.
if (sofar + numbers[i] + (6 - current_level) * numbers[i] > s) {
return;
}
// Assume the current choice.
v[current_level] = numbers[i];
if (current_level == 6) {
if (sofar + numbers[i] == s) {
write_sol_and_quit();
} else {
return;
}
} else {
gen(current_level + 1, sofar + numbers[i], i, end);
}
}
}
int main()
{
ifstream in("loto.in");
in >> n >> s;
for (int i = 0; i < n; ++i) {
int x;
in >> x;
numbers.push_back(x);
}
std::sort(numbers.begin(), numbers.end());
gen(1, 0, 0, numbers.size() - 1);
// If it returned, that's bad news.
ofstream out("loto.out");
out << "-1\n";
out.close();
// std::cerr << "Iterations: " << it << std::endl;
return 0;
}