Cod sursa(job #3128595)

Utilizator florinilie324Ilie Florin Alexandru florinilie324 Data 10 mai 2023 08:12:57
Problema Loto Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

bool backtracking(int N, int S, int pas, int suma_curenta, vector<int> &numere, vector<int> &combinatie) {
    if (pas == 6) {
        if (suma_curenta == S) {
            return true;
        }
        return false;
    }

    for (int i = 0; i < N; ++i) {
        combinatie[pas] = numere[i];
        if (backtracking(N, S, pas + 1, suma_curenta + numere[i], numere, combinatie)) {
            return true;
        }
    }

    return false;
}

int main() {
    int N, S;
    ifstream fin("loto.in");
    ofstream fout("loto.out");

    fin >> N >> S;
    vector<int> numere(N);
    for (int i = 0; i < N; ++i) {
        fin >> numere[i];
    }

    vector<int> combinatie(6);
    if (backtracking(N, S, 0, 0, numere, combinatie)) {
        for (int i = 0; i < 6; ++i) {
            fout << combinatie[i] << ' ';
        }
    } else {
        fout << -1;
    }

    fin.close();
    fout.close();
    return 0;
}