Cod sursa(job #1142686)

Utilizator dec0o0dinu pinu dec0o0 Data 14 martie 2014 02:29:48
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

int n, s;
vector<int> v;
vector<int> val;
int afiseaza(int);
ifstream f("loto.in");
ofstream g("loto.out");

int main(){

	f>>n;
	f>>s;
	val = vector<int> (n);
	
	for (int i=0; i<n; i++) {
		f>>val[i];
	}

	v = vector<int> (0);

	for (int i=0; i<n; i++)
		for (int j=0; j<n; j++)
			for (int k=0; k<n; k++)
				v.push_back(val[i]+val[j]+val[k]);

	sort(v.begin(), v.end());

	bool gasit = false;
	int sol = -1;
	int nn = v.size();
	int cursor = nn-1;

	for (int i=0; i<nn && !gasit; i++)
		if (v[i] + v[cursor] == s) {
			gasit = true;
			afiseaza(v[i]);
			afiseaza(v[cursor]);
		}
		else
			while (v[i] + v[cursor] > s && cursor) cursor--;

	if (!gasit)
		g<<"-1";

	f.close();
	g.close();
	return 0;
}

int afiseaza(int suma) {
	for (int i=0; i<n; i++)
		for (int j=0; j<n; j++)
			for (int k=0; k<n; k++)
				if ((val[i] + val[j] + val[k]) == suma) {
					g << val[i] << ' ' << val[j] << ' ' << val[k] << ' ';
					return 1;
				}
	return 0;
}