Cod sursa(job #413138)

Utilizator darkseekerBoaca Cosmin darkseeker Data 7 martie 2010 20:07:47
Problema Loto Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.93 kb
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
int k,st[7],n,a[100];
unsigned long int s,x=0;
fstream f("loto.in",ios::in);
fstream g("loto.out",ios::out);
bool ok=0;
int init()
{
    st[k]=-1;
}
int succesor()
{
    if(st[k]!=-1)
    x=x-a[st[k]];
    st[k]++;
    if(st[k]<n)
    return 1;
    else
    return 0;
}
int valid()
{
     x+=a[st[k]];
    if(x>s)
    return 0;
    else
    return 1;
}
int solutie()
{
    if(k==6&&x==s)
    {
                  ok=1;
                  return 1;
                  
                  }
    else
    return 0;
}
void tipar()
{
     for(int i=1;i<=k;i++)
     g<<a[st[i]]<<" ";
     }
void back()
{
     bool as,ev;
     k=1;
     init();
     while(k>0&&ok==0)
     {
               do{
                    as=succesor();
                    if(as)
                    ev=valid();
                    }while(as&&!ev);
                    if(as)
                    if(solutie())
                    tipar();
                    else
                        if(k>=6)
                        {
                                k--;
                                x=x-a[st[k+1]];
                                }
                                else
                                {
                        
                        k++;
                        init();
                        }
                        else
                        {k--;
                        x=x-a[st[k+1]];
                        }
                        }
                        if(ok==0)
                        g<<-1;
}
int main()
{
    int max=0;
    f>>n;
    f>>s;
    for(int i=0;i<=n-1;i++)
    {
            f>>a[i];
            if(a[i]>max)
            max=a[i];
            }
    if(max*6<s)
    g<<-1;
    else
    back();
    return 0;
}