Cod sursa(job #588366)

Utilizator predator5047Butiu Alexandru Octavian predator5047 Data 7 mai 2011 19:25:11
Problema Fractii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <cstdio>
#include <algorithm>
FILE *g=fopen("loto.in","r");
FILE *z=fopen("loto.out","w");
using namespace std;
int v[160],s,st[160],n,s2=0;
bool ok=false;
void qsort(int l,int r)
{
    int i=l,j=r,t;
    t=v[(l+r)/2];
    while(i<=j)
    {
        while(v[i]<t)
        i++;
        while(v[j]>t)
        j--;
        if(i<=j)
        {
            swap(v[i],v[j]);
            i++;
            j--;
        }
    }
    if(l<j)
    qsort(l,j);
    if(i<r)
    qsort(i,r);
}
void back(int niv)
{
    int i;
    if(ok==false)
    for(i=1;i<=n;i++)
    {

        if(s2+v[i]<=s)
        {
            st[niv]=v[i];
            s2+=v[i];
            if(niv==6 and s2==s and ok==false)
            {
                for(int j=1;j<=6;j++)
                fprintf(z,"%d ",st[j]);
                fclose(z);
                ok=true;
                return ;
            }
            else
            {

                back(niv+1);
                s2-=v[i];
            }

        }
        else return ;
    }
}

int main()
{
    int max=-564;
    fscanf(g,"%d%d",&n,&s);
    for(int i=1;i<=n;i++)
    {
        fscanf(g,"%d",&v[i]);
        if(max<v[i])
        max=v[i];
    }
    fclose(g);
    if(max*6<s)
    {
        fprintf(z,"-1");
        return 0;
    }
    qsort(1,n);
    back(1);
    if(ok==false)
    fprintf(z,"-1");
    return 0;
}