Cod sursa(job #1394106)

Utilizator kagy85Kolumban Antal kagy85 Data 20 martie 2015 00:02:10
Problema Loto Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <map>


using namespace std;
//az n meximalis erteke
const unsigned short MaxN=100, LottoNr=6;

typedef struct
{
    unsigned long x[3];
} state;

typedef map <unsigned long, state> num_map;

num_map mp; //eltaroljuk azt is hogy hanyadik volt az elozo
unsigned long a[MaxN], s;
unsigned short n;

int comp_ulong(const void * pv1, const void * pv2)
{
    unsigned long l1=*((unsigned long *)pv1), l2=*((unsigned long *)pv2);

    return l1<l2 ? -1 : (l1==l2 ? 0 : 1);
}

state gen_state(unsigned long x0, unsigned long x1, unsigned long x2)
{
    state tst;

    tst.x[0]=x0;
    tst.x[1]=x1;
    tst.x[2]=x2;

    return tst;
}

int main(void)
{
    ifstream fi("lotto.in", ios::in);
    ofstream fo("lotto.out", ios::out);
    unsigned short i, j, k, l;
    num_map::iterator it, it2;
    bool won=false;

    fi>>n>>s;
    for (i=0; i!=n; i++)
        fi>>a[i];
    for (i=0; i!=n; i++)
        for (j=0; j!=n; j++)
            for (k=0; k!=n; k++)
                mp[a[i]+a[j]+a[k]]=gen_state(a[i], a[j], a[k]);
    for (it=mp.begin(); (it!=mp.end())&&(!won); it++)
    {
        if ((it2=mp.find(s-(it->first))) !=mp.end())
        {
            won=true; //megvan a nyero kombinacio
            for (l=0; l!=3; l++)
                fo<<it->second.x[l]<<' '<<it2->second.x[l]<<' ';
        }
    }
    if (!won)
        fo<<-1;
    fo.close();
    fi.close();

    return 0;
}