Cod sursa(job #2625141)

Utilizator CoakazeRotaru Catalin Coakaze Data 5 iunie 2020 19:12:27
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

struct loto
{
    int a, b, c, sum;
};

bool cmp(loto x, loto y)
{
    return x.sum < y.sum;
}

loto d[1000001];

int main()
{
    ifstream f("loto.in");
    ofstream g("loto.out");
    int v[101], ct = 0, n, s, i, j, k;
    f>>n>>s;
    for(int i=1; i<=n; i++)
        f>>v[i];
    for(i=1; i<=n; i++)
        for(j=i; j<=n; j++)
            for(k=j; k<=n; k++)
            {
                d[++ct].a = v[i];
                d[ct].b = v[j];
                d[ct].c = v[k];
                d[ct].sum = v[i]+v[j]+v[k];
            }
    sort(d + 1, d + ct + 1, cmp);
    int x = 1, y = ct, gasit = 0;
    while(x <= y)
    {
        if(d[x].sum + d[y].sum == s)
        {
            gasit = 1;
            g<<d[x].a<<" "<<d[x].b<<" "<<d[x].c<<" "<<d[y].a<<" "<<d[y].b<<" "<<d[y].c;
            return 0;
        }
        else if(d[x].sum + d[y].sum<s)
            x++;
        else
            y--;
    }
    if(!gasit)
        g<<-1;
    return 0;
}