Cod sursa(job #2288782)

Utilizator sabinpocrisSabin P sabinpocris Data 23 noiembrie 2018 21:16:54
Problema Loto Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <bits/stdc++.h>

using namespace std;


ifstream in("loto.in");
ofstream out("loto.out");

vector<int> givenNumbers;
bool itWorked = false;
int nr;

void findSum(int sum, string past, int limit){
    if (sum > nr || limit < 0 || itWorked)
        return;
    
    if (sum == nr && limit == 0){
        out << past << "\n";
        itWorked = true;
        exit(EXIT_SUCCESS);
        return;
    }

    for (int item : givenNumbers){
        int temp = sum + item;
        string str;
        
        if (past == ""){
            str = to_string(item);
        } else {
            str = past + " " + to_string(item);
        }

        findSum(temp, str, limit - 1);
    }
}

int main(){
    int n, temp;

    in >> n >> nr;
    while(n--){
        in >> temp;
        givenNumbers.push_back(temp);
    }

    findSum(0, "", 6);
    
    done:
    if (!itWorked){
        out << "-1\n";
    }
    
    return 0;
}