Cod sursa(job #3130846)

Utilizator opreaopreacalin@gmail.comCalin Oprea [email protected] Data 18 mai 2023 18:23:04
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <stdio.h>
#include <algorithm>
#include <fstream>
#define maxn 101

using namespace std;

long sol[maxn * maxn * maxn];
long v[maxn];
long n, s;
ifstream in("loto.in");
ofstream out("loto.out");

void afisare(long a) {
    long i, j, l;
    bool ok=0;
    for( i = 1;i <= n and ok==0; ++i) {
        for(j = 1;j <= n and ok==0; ++j) {
            for(l = 1;l <= n and ok==0; ++l) {
                if(v[i] + v[j] + v[l] == sol[a])
                {
                    out<<v[i]<<" "<<v[j]<<" "<<v[l]<<" ";
                    ok=1;
                }
            }
        }
    }
}

int main()
{
    long i, j, l, t;

    in>>n>>s;
    for(i = 1;i <= n; ++i)
        in>>v[i];
    long nr = 0;
    for(i = 1;i <= n; ++i) {
        for(j = 1; j <= n; ++j) {
            for(l = 1;l <= n; ++l) {
                sol[nr++]=v[i] + v[j] + v[l];
            }
        }
    }
    sort(sol, sol + nr);
    t = nr - 1;
    for(i = 0;i < nr && t >= 0; ++i) {
        while(sol[i] + sol[t] > s && t >= 0) {
            t--;
        }
        if (t < 0) {
            break;
        }
        if (sol[i] + sol[t] == s) {
            afisare(i);
            afisare(t);
            return 0;
        }
    }
    out<<"-1";
    return 0;
}