Cod sursa(job #1699600)

Utilizator panteapaulPantea Paul panteapaul Data 7 mai 2016 21:39:33
Problema Loto Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.72 kb
#include <iostream>
#include <fstream>
#include <deque>
#include <algorithm>

using namespace std;

int n, s;
int nrs[100];
int pairs[5050];
pair<int,int> indx[5050];
int stak[6];
ifstream in("loto.in");
ofstream out("loto.out");

int main()
{
    bool sol = false;
    int a, b, c, d, e, f, x, i, k, j, N;
    int sa = 0, sb = 0, sc = 0, sd = 0, se = 0, sf = 0;
    in>>n>>s;

    k = 0;
    for (i=0; i<n; i++) {
        in>>x;
        nrs[i] = x;

        for (j=0; j<=i; j++) {
            pairs[k] = x + nrs[j];
            indx[k] = pair<int,int>(j, i);
            cout<<pairs[k]<<" ";
            k++;
        }
    }

    N = n*(n+1)/2;
    sort(pairs, pairs+N);

    for (a=0; a<N; a++) {
        sa = pairs[a];
        cout<<sa<<" ";

        if (pairs[a] * 3 <= s)
        for (b=a; b<N; b++) {
            sb = sa + pairs[b];
            cout<<sb<<" ";

            if (sa + pairs[b] * 2 <= s)
            for (c=b; c<N; c++) {
                sc = sb + pairs[c];
                cout<<sc<<endl;

                if (sc == s) {
                    sol = true;
                    goto done;
                }
                else if (sc > s) {
                    break;
                }
            }
        }
    }

    done:
    if (!sol) {
        out<<-1;
    }
    else {
        stak[0] = nrs[indx[a].first];
        stak[1] = nrs[indx[a].second];
        stak[2] = nrs[indx[b].first];
        stak[3] = nrs[indx[b].second];
        stak[4] = nrs[indx[c].first];
        stak[5] = nrs[indx[c].second];

        sort(stak, stak+6);
        for (i=0; i<6; i++) {
            out<<stak[i]<<" ";
        }
    }

    in.close();
    out.close();
    return 0;
}