Cod sursa(job #1415388)

Utilizator stanciuandreiStanciulescu Andrei stanciuandrei Data 4 aprilie 2015 15:01:52
Problema Loto Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("loto.in");
ofstream out("loto.out");
int main()
{
    int n, s;
    bool sol=false;
    in>>n>>s;
    int numere[n];
    for(int i=0;i<n;i++)
    {
        in>>numere[i];
    }
    for(int a=0;a<n;a++)
    {
        for(int b=0;b<n;b++)
        {
            for(int c=0;c<n;c++)
            {
                for(int d=0;d<n;d++)
                {
                    for(int e=0;e<n;e++)
                    {
                        int sp=numere[a]+numere[b]+numere[c]+numere[d]+numere[e];
                        int dif=s-sp;
                        bool found=false;
                        int l=0, r=n-1;
                        while(!found&&l<r)
                        {
                            if(numere[(r+l)/2]==dif)
                                found=true;
                            else
                            {
                                if(numere[(r+l)/2]>dif)
                                    r=(r+l)/2;
                                else
                                    l=(r+l)/2+1;
                            }
                        }
                        if(found&&!sol)
                        {
                            out<<numere[a]<<" "<<numere[b]<<" "<<numere[c]<<" "<<numere[d]<<" "<<numere[e]<<" "<<dif<<"\n";
                            sol=true;
                        }
                    }

                }

            }

        }

    }
    if(!sol)
        out<<-1<<"\n";
    return 0;
}