Pagini recente » Cod sursa (job #2706239) | Cod sursa (job #2778583) | Cod sursa (job #1874977) | Cod sursa (job #852436) | Cod sursa (job #1306994)
#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)
{
Node newEntry;
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;
}