Pagini recente » Cod sursa (job #1832622) | Cod sursa (job #2763638) | Cod sursa (job #1116787) | Cod sursa (job #3284337) | Cod sursa (job #1734182)
#include <iostream>
#include <fstream>
using namespace std;
const int NMAX = 101;
const int hashMax = 8960454;
int v[NMAX];
int n,s;
struct node
{
int value;
int i1,i2,i3;
node* next = NULL;
};
typedef node* hashTable[hashMax];
hashTable H;
int hashKey(int value)
{
int k = 8960453;
return value % k;
}
void insertNumber(int value,int i1, int i2, int i3)
{
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;
}
}
node* findValue(int value)
{
int key = hashKey(value);
node *currentNode = H[key];
while(currentNode != NULL)
{
if(currentNode->value = value)
return currentNode;
currentNode = currentNode->next;
}
return NULL;
}
int main()
{
node* foundNode = new node;
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++)
{
insertNumber(v[i] + v[j] + v[k],i,j,k);
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;
}