Cod sursa(job #779926)

Utilizator fhandreiAndrei Hareza fhandrei Data 19 august 2012 02:07:11
Problema Loto Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
//Include
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;

//Definitii
#define pb push_back

//Structuri
struct mystr
{
	int value;
	int first, second, third;
}newElement, searched;

//Clase
class compare
{
	public:
	bool operator() (mystr a, mystr b)
	{	return a.value < b.value;	}
};

//Variabile
ifstream in("loto.in");
ofstream out("loto.out");

int eNum, sum;
int numbers[101];

vector<mystr> sums;
vector<mystr>::iterator it1, it2, end;

//Main
int main()
{
	in >> eNum >> sum;
	for(int i=1 ; i<=eNum ; ++i)
		in >> numbers[i];
	
	for(int i=1 ; i<=eNum ; ++i)
		for(int j=1 ; j<=eNum ; ++j)
			for(int k=1 ; k<=eNum ; ++k)
			{
				newElement.value = numbers[i] + numbers[j] + numbers[k];
				newElement.first = numbers[i];
				newElement.second = numbers[j];
				newElement.third = numbers[k];
				sums.pb(newElement);
			}
	
	sort(sums.begin(), sums.end(), compare());
	end = sums.end();
	
	for(it1=sums.begin() ; it1!=end ; ++it1)
	{
		searched.value = sum - it1->value;
		it2 = lower_bound(it1, end, searched, compare());
			if(it2->value == searched.value)
			{
				out << it1->first << ' ' << it1->second << ' ' << it1->third << ' ' << it2->first << ' ' << it2->second << ' ' << it2->third << '\n';
				in.close();
				out.close();
				return 0;
			}
	}
	
	
	out << -1 << '\n';
	in.close();
	out.close();
	return 0;
}