Cod sursa(job #447152)

Utilizator voikybodea voichita voiky Data 27 aprilie 2010 20:38:54
Problema Loto Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include<fstream>
using namespace std;

int n,s,x[101],suma[1000001],p=0,indice[3][1000001];

void ordon()
{
	int i,j,aux;
	for(i=1;i<n;i++)
		for(j=i+1;j<=n;j++)
			if(x[i]>x[j]){aux=x[i];x[i]=x[j];x[j]=aux;}
}

int main()
{
	ifstream f("loto.in");ofstream g("loto.out");
	int i,j,k,max=-1;
	f>>n>>s;for(i=1;i<=n;i++)f>>x[i];ordon();
//	suma[++p]=x[1];
//	suma[++p]=x[2];
//	suma[++p]=max=x[3];
//	if(x[1]+x[2]>max)suma[++p]=max=x[1]+x[2];
//	if(x[1]+x[2]>max)suma[++p]=max=x[1]+x[2];
//	if(x[1]+x[2]>max)suma[++p]=max=x[1]+x[2];
	for(i=1;i<=n;i++)
		for(j=i;j<=n;j++)
			for(k=j;k<=n;k++)
			{
				suma[++p]=x[i]+x[j]+x[k];
				if(suma[p]>max)
				{
					max=suma[p];
					indice[0][p]=i;indice[1][p]=j;indice[2][p]=k;
				}
				else p--;
			}	
	for(i=1;i<=p;i++)
		for(j=1;j<=p;j++)
			if(suma[i]+suma[j]==s)
			{
				g<<x[indice[0][i]]<<' '<<x[indice[1][i]]<<' '<<x[indice[2][i]]<<' '<<x[indice[0][j]]<<' '<<x[indice[1][j]]<<' '<<x[indice[2][j]];
				f.close();g.close();
				return 0;
			}	
	g<<-1;
	f.close();g.close();
	return 0;
}