Cod sursa(job #349800)

Utilizator zloteanu.adrianzloteanu adrian nichita zloteanu.adrian Data 21 septembrie 2009 16:22:58
Problema Loto Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
#include<cstdio>
#include<algorithm>
#include<vector>
#include<fstream>
using namespace std;

int N;
int v[105],S;
struct Numere
{
	int a,b,c,suma;
};

vector <Numere> a;

int cmp(const Numere &x,const Numere &y)
{ 
	return x.suma<y.suma;
}

int main()
 {
	fstream q("loto.in");
	ofstream w("loto.out");
	
	int i,j,k;
	q>>N>>S;
	for(i=1;i<=N;i++)
		q>>v[i];;
	for(i=1;i<=N;i++)
		for(j=i;j<=N;j++)
			for(k=j;k<=N;k++)
			{
				 Numere x;
				 x.a=v[i];
				 x.b=v[j];
				 x.c=v[k];
				 x.suma=v[i]+v[j]+v[k];
				 a.push_back(x);
			}
	sort(a.begin(),a.end(),cmp);
	i=0;
	j=a.size()-1;
	while(i<=j)
	{
		int dif=S-a[i].suma;
		while(a[j].suma>dif) j--;
		if(a[j].suma==dif)
		{
			w<<a[i].a<<" "<<a[i].b<<" "<<a[i].c<<" "<<a[j].a<<" "<<a[j].b<<" "<<a[j].c;
			return 0;
		}
		i++;
	}
	w<<"-1";
	return 0;
}