Cod sursa(job #3131158)

Utilizator dariutTache Daria dariut Data 19 mai 2023 13:06:00
Problema Loto Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <iostream>
#include <fstream>
#include <vector>

void findCombination(std::vector<int>& loto, std::vector<int>& chosen, int sum, std::vector<int>& result) {
    if (sum == 0 && chosen.size() == 6) {
        result = chosen;
        return;
    }

    if (sum < 0 || chosen.size() == 6) {
        return;
    }

    for (int num : loto) {
        chosen.push_back(num);
        findCombination(loto, chosen, sum - num, result);
        chosen.pop_back();
    }
}

int main() {
    std::ifstream f("loto.in");
    std::ofstream g("loto.out");

    int N, S;
    f >> N >> S;

    std::vector<int> loto(N);
    for (int i = 0; i < N; ++i) {
        f >> loto[i];
    }

    std::vector<int> result;
    std::vector<int> chosen;

    findCombination(loto, chosen, S, result);

    if (result.size() == 6) {
        for (int num : result) {
            g << num << ' ';
        }
    }
    else {
        g << -1;
    }

    f.close();
    g.close();

    return 0;
}