Cod sursa(job #1206833)

Utilizator mihaimusatMihai Musat mihaimusat Data 11 iulie 2014 12:28:38
Problema Loto Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <fstream>
#include<set>

using namespace std;

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

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

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

int eNum, sum;
int numbers[101];

set<mystr, compare> sums;
set<mystr, compare>::iterator it1, it2, end;

int main()
{
	in >> eNum >> sum;
	for(int i=1 ; i<=eNum ; ++i)
		in >> numbers[i];

	for(int i=0 ; ++i<=eNum ;)
		for(int j=i-1 ; ++j<=eNum ;)
			for(int k=j-1 ; ++k<=eNum ;)
			{
				newElement.value = (newElement.first = numbers[i]) + (newElement.second = numbers[j]) + (newElement.third = numbers[k]);
				sums.insert(newElement);
			}

	end = sums.end();

	for(it1=sums.begin() ; it1!=end ; ++it1)
	{
		searched.value = sum - it1->value;
			if((it2 = sums.find(searched)) != end)
			{
				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;
}