Cod sursa(job #2213667)

Utilizator bisarenaBudai Istvan bisarena Data 16 iunie 2018 19:44:38
Problema Loto Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int x[100], v[6], N, S, ok=0;

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

int comp(const void* a, const void* b)
   {
     int* arg1 = (int*) a;
     int* arg2 = (int*) b;
     if( *arg1 < *arg2 ) return -1;
     else if( *arg1 == *arg2 ) return 0;
     else return 1;
   }

bool accept(int p)
{
   return ((p<=5 && x[v[p]]<S));
}

void Back(int i)
{
    for (int j=0; j<N; j++)
    {
        v[i]=j;
        if (accept(i))
        {
            if (i<5) Back(i+1);
            else
            {
                int T=0;
                for (int k=0; k<=5; ++k) T += x[v[k]];
                if (T==S)
                    {
                        for(int k=0; k<=5; ++k) g<<x[v[k]]<<" ";
                        ok=1;
                        exit(0);
                    }
            }
        }
    }
}

int main()
{
    f >> N >> S;
    for (int i=0; i<=N-1; ++i) f >> x[i];
    qsort(x, N, sizeof(int), comp);
    int lim = S/6;
    int q=N-1;
    while (x[q]>lim) q--;
    if (x[q]==lim) for (int i=1; i<=6; i++) g << lim << " ";
    else
    {
        Back(0);
        if(ok==0) g<<-1;
    }
    f.close(); g.close();
    return 0;
}