Cod sursa(job #66095)

Utilizator M@2Te4iMatei Misarca M@2Te4i Data 15 iunie 2007 16:12:18
Problema Loto Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.73 kb
#include<stdio.h>

long a[101],s;
int n,gasit=0,q[101];

void citire()
{
freopen("loto.in","r",stdin);     
scanf("%d%ld", &n, &s);
for (int i=0; i<n; i++)
    scanf("%ld", &a[i]);
fclose(stdin);
}

void afiseaza()
{
freopen("loto.out","w",stdout);
for (int i=0; i<6; i++)
    printf("%ld ",a[q[i]]);
gasit=1;
fclose(stdout);
}

void back(int w)
{
if (w>5)
   {
   if (s==0)
       afiseaza();
   }
   else {
	for (int i=n-1; i>=0; i--)
	    {
	    s-=a[i];
	    q[w]=i;
	    if (s>=0 && gasit==0)
	       back(w+1);
	    s+=a[i];
	    }
	}
}

int main()
{
citire();
back(0);
if (gasit==0)
   {
   freopen("loto.out","w",stdout);     
   printf("-1");
   fclose(stdout);
   }
return 0;   
}