Cod sursa(job #2742839)

Utilizator bianca_voicuBianca Voicu bianca_voicu Data 21 aprilie 2021 22:58:54
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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

struct nrLoto {
    int suma, a, b, c;
};

nrLoto rez[10000001];

bool comparator(nrLoto d, nrLoto e) {
    return d.suma < e.suma;
}

int main() {
    int n, v[101], s, i, j, k, cnt = 0;
    //citire
    f >> n >> s;
    for (i = 1; i <= n; i++)
        f >> v[i];

    //crearea sumelor
    for (i = 1; i <= n; i++)
        for (j = i; j <= n; j++)
            for (k = j; k <= n; k++) {
                cnt++;
                rez[cnt].a = v[i];
                rez[cnt].b = v[j];
                rez[cnt].c = v[k];
                rez[cnt].suma = v[i] + v[j] + v[k];
            }
    //sortare
    sort(rez + 1, rez + cnt + 1, comparator);

    int x = 1, y = cnt, bec = 0;
    //gasirea solutiei
    while (x <= y) {
        if (rez[x].suma + rez[y].suma == s) {
            bec = 1;
            o << rez[x].a << " " << rez[x].b << " " << rez[x].c << " " << rez[y].a << " " << rez[y].b << " "
                 << rez[y].c;
            break;
        } else if (rez[x].suma + rez[y].suma < s)
            x++;
        else
            y--;
    }
    if (!bec)
        o << -1;

    f.close();
    o.close();
    return 0;
}