Cod sursa(job #1734200)

Utilizator codebreaker24Tivadar Ionut codebreaker24 Data 26 iulie 2016 19:06:08
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.09 kb
#include <iostream>
#include <fstream>
using namespace std;
const int NMAX = 101;
const int hashMax = 105000;

int v[NMAX];
int n;
long int s;
struct node
{
    long int value;
    int i1,i2,i3;
    node* next = NULL;

};
typedef node* hashTable[hashMax];
hashTable H;


int hashKey(int value)
{
    int k = 104999;
    return value % k;

}

node* findValue(long int value)
{

    int key = hashKey(value);
    node *currentNode = H[key];

    while(currentNode != NULL)
    {
        if(currentNode->value == value)
            return currentNode;
       currentNode = currentNode->next;

    }

    return NULL;
}
void insertNumber(long int value,int i1, int i2, int i3)
{

    if(findValue(value))
        return;

    int key = hashKey(value);
    node *currentNode = H[key];

    node *newnode = new node;
    newnode->next= NULL;
    newnode->value = value;
    newnode->i1 = i1;
    newnode->i2 = i2;
    newnode->i3 = i3;

     if(H[key] == NULL)
     {
         H[key] = newnode;
     }
     else
      {

        while(currentNode->next != NULL)
        {

            currentNode  = currentNode ->next;
        }
        currentNode->next = newnode;
      }

}




int main()
{
    long int halfsum;
    node* foundNode = NULL;
    ifstream fin ("loto.in");
    ofstream fout ("loto.out");
    fin >> n >> s;
    for(int i=0; i<n; i++)
        fin >> v[i];


    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
         for(int k=0; k<n; k++)
         {
             halfsum = v[i] + v[j] + v[k];
             insertNumber(halfsum,i,j,k);

             if(halfsum < s)
                foundNode = findValue(s - (v[i] + v[j] + v[k]));
             if(foundNode != NULL)
             {
                 fout << v[i] << ' ' << v[j] << ' ' <<v[k]<< ' '<<v[foundNode->i1] << ' '<< v[foundNode->i2]<<' ' << v[foundNode->i3] <<'\n';
                 i = j = k = n;
             }
         }

         if(foundNode == NULL)
            fout << -1 << '\n';


         fin.close();
         fout.close();

   return 0;

}