Pagini recente » Cod sursa (job #2658560) | Cod sursa (job #68348) | Cod sursa (job #1121742) | Cod sursa (job #1691518) | Cod sursa (job #1307001)
#include <fstream>
#include <unordered_map>
using namespace std;
struct Node
{
int indexI, indexJ, indexK;
};
int main()
{
int N, S, i, j, k;
ifstream f("loto.in");
f >> N >> S;
int a[N + 1];
for (i = 1; i <= N; i++)
f >> a[i];
f.close();
unordered_map<int, Node*> map;
int sum;
for (i = 1; i <= N; i++)
for (j = 1; j <= N; j++)
for (k = 1; k <= N; k++)
{
sum = a[i] + a[j] + a[k];
if (sum <= S && !map[sum])
{
Node *newEntry = new Node;
newEntry->indexI = i;
newEntry->indexJ = j;
newEntry->indexK = k;
map[sum] = newEntry;
}
}
bool found = false;
unordered_map<int, Node*>::iterator pairIt;
ofstream g("loto.out");
for (unordered_map<int, Node*>::iterator it = map.begin(); it != map.end() && !found; it++)
{
pairIt = map.find(S - it->first);
if (pairIt != map.end())
{
found = true;
g << a[it->second->indexI] << " " << a[it->second->indexJ] << " " << a[it->second->indexK] << " ";
g << a[pairIt->second->indexI] << " " << a[pairIt->second->indexJ] << " " << a[pairIt->second->indexK];
}
}
if (!found)
g << -1;
g.close();
return 0;
}