Cod sursa(job #2625424)

Utilizator DariaCretuCretu Daria Stefana DariaCretu Data 5 iunie 2020 22:44:02
Problema Loto Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.78 kb
// LOTO.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;
ifstream f("loto.in");
ofstream g("loto.out");
struct suma {
	int b1, b2, b3, sum;
}; 

suma bilete[1000005];

bool comparare(suma m, suma n)
{
	return m.sum < n.sum;
}

int n, v[1001], k;
int main()
{
	f >> n >> k;
	for (int i = 1; i <= n; i++)
		f >> v[i];
	int num = 0;
	for (int i = 1; i <= n; i++)
		for (int j = i; j <= n; j++)
			for (int k = i; k <= n; k++)
			{
				num++;
				bilete[num].b1 = v[i];
				bilete[num].b2 = v[j];
				bilete[num].b3 = v[k];
				bilete[num].sum = v[i] + v[j] + v[k];
			}
	sort(bilete + 1, bilete + num, comparare);
	int i = 0, j = num, ok = 0;
	while (i <= j)
	{
		if (bilete[i].sum + bilete[j].sum == k)
		{
			g << bilete[i].b1 << " " << bilete[i].b2 << " " << bilete[i].b3 << " " << bilete[j].b1 << " " << bilete[j].b2 << " " << bilete[j].b3;
			ok = 1;
			break;
		}
		else 
			if (bilete[i].sum + bilete[j].sum > k)
			j--;
		else
			i++;
		}
		if (ok == 0)
			g << -1;
			return 0;


	}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file