Cod sursa(job #1285068)

Utilizator nosurrender99Bura Bogdan nosurrender99 Data 7 decembrie 2014 01:35:19
Problema Loto Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <fstream>
#include <iostream>
#include <stdio.h>      /* printf, fopen */
#include <stdlib.h>
using namespace std;

fstream f("loto.in", ios::in), g("loto.out", ios::out);

int sol[7];

bool isSubsetSum(int set[], int n, int sum,int count)
{
    if (sum == 0 && sol[1] !=0 && sol[2] !=0 && sol[3] !=0 && sol[4] !=0 && sol[5] !=0 && sol[6] !=0)
    {
        for(int i=6;i>=1;i--)
            printf("%d ",sol[i]);
        exit(0);
    }
    if (count==7)
        return false;
    if (n == 0 && sum != 0)
        return false;

   // If last element is greater than sum, then ignore it
   if (set[n-1] > sum)
     return isSubsetSum(set, n-1, sum,count);

   /* else, check if sum can be obtained by any of the following
      (a) including the last element
      (b) excluding the last element   */
   bool first = isSubsetSum(set, n-1, sum,count);
   sol[count]=set[n-1];
   return first || isSubsetSum(set, n, sum-set[n-1],count+1);
}

// Driver program to test above function
int main()
{
  int set[101];
  int sum, n;
  f>>n>>sum;
  for(int i=0;i<n;i++)
  {
        f>>set[i];
  }
  if (isSubsetSum(set, n, sum,1) == false)
     printf("-1");
  return 0;
}