Cod sursa(job #2744347)

Utilizator RoberttoPopescu Paullo Robertto Karloss Robertto Data 24 aprilie 2021 15:12:27
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <iostream>
#include <unordered_map>
#include <fstream>

using namespace std;

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

int main() {
    unordered_map<int, int> hash;

    int s, n, v[100];
    fin >> n >> s; // citim numarul de numere si suma la care trebuie sa ajungem
    for (int i = 0; i < n; i++) {
        fin >> v[i]; // citim numerele care trebuie sa dea suma S pentru a fi biletul castigator
    }
    int ok = 0;
    int s3 = 0;
    for (int i = 0; i < n && ok == 0; i++) {
        for (int j = i; j < n && ok == 0; j++) {
            for (int k = j; k < n && ok == 0; k++) {
                s3 = v[i] + v[j] + v[k]; // calculam suma a 3 numere si o retinem in s3
                hash.insert({s3, n * n * i + n * j + k}); //
                auto it = hash.find(s - s3); // pointer catre suma complementara a lui s3
                if (it != hash.end()) {
                    ok = 1;
                    int nr = it->second;
                    fout << v[i] << " " << v[j] << " " << v[k] << " " << v[nr % n] << " ";
                    nr /= n;
                    fout << v[nr % n] << " ";
                    nr /= n;
                    fout << v[nr % n] << "\n";
                }
            }
        }
    }
    if (ok == 0)
        fout << -1 << "\n";
    fin.close();
    fout.close();
    return 0;
}