Cod sursa(job #2624547)

Utilizator andreea.bucurBucur Andreea andreea.bucur Data 4 iunie 2020 23:09:50
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("loto.in");
ofstream g("loto.out");
struct suma
{
    int a, b, c, s;
} w[1000005];
bool com(suma x, suma y)
{
    return x.s < y.s;
}

int n,  v[101], S;
int main()
{
    f >> n >> S;
    for( int i = 1; i <= n; i++)
        f >> v[i];
    int nr = 0;
    for(int i = 1; i<= n; i++)
        for(int j = i; j <= n; j++)
            for(int k = j; k <= n; k++)
            {
                nr++;
                w[nr].a = v[i];
                w[nr].b = v[j];
                w[nr].c = v[k];
                w[nr].s = v[i] + v[j] + v[k];
            }
    sort(w+1, w+nr+1, com);
    int i = 1, j = nr;
    int ok = 0;
    while(i <= j)
    {
        if(w[i].s + w[j].s == S)
        {
            ok = 1;
            g << w[i].a << " " << w[i].b << " " << w[i].c << " " << w[j].a << " " << w[j].b << " " << w[j].c;
            break;
        }

        else if(w[i].s + w[j].s < S)
            i++;
        else
            j--;
    }
    if(ok == 0)
        g << -1;
    return 0;
}