Cod sursa(job #1731463)

Utilizator Vlad_317Vlad Panait Vlad_317 Data 18 iulie 2016 23:54:42
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <stdio.h>
#include <algorithm>
using namespace std;

const int MAXN = 100;

int v[MAXN + 1], cnt = 0;
struct sums
{
    int val, a, b, c;
} sum[MAXN * MAXN * MAXN + 100];

bool cmp(sums x, sums y)
{
    return x.val < y.val;
}


int main()
{
    FILE *fin, *fout;

    fin = fopen("loto.in", "r");
    fout = fopen("loto.out", "w");

    int n, s;

    fscanf(fin, "%d%d", &n, &s);

    for(int i = 1; i <= n; i++)
        fscanf(fin, "%d", &v[i]);

    for(int i = 1; i <= n; i++)
        for(int j = i; j <= n; j++)
            for(int k = j; k <= n; k++)
            {
                sum[++cnt].val = v[i] + v[j] + v[k];
                sum[cnt].a = v[i];
                sum[cnt].b = v[j];
                sum[cnt].c = v[k];
            }

    sort(sum + 1, sum + cnt + 1, cmp);

    int p = cnt;

    for(int i = 1; i <= cnt; i++)
    {
        while(sum[i].val + sum[p].val > s)
            p--;
        if(sum[i].val + sum[p].val == s)
        {
            fprintf(fout, "%d %d %d %d %d %d", sum[i].a, sum[i].b, sum[i].c, sum[p].a, sum[p].b, sum[p].c);
            return 0;
        }
    }

    fprintf(fout, "-1");
    return 0;
}