Pagini recente » Cod sursa (job #1303234) | Cod sursa (job #264646) | Cod sursa (job #520778) | Cod sursa (job #1394332) | Cod sursa (job #2623673)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("loto.in");
ofstream out("loto.out");
int i,n,s,vect[1000001],j,o,val,bilet,poz;
bool ok;
struct loto
{
int x,y,z,suma;
}v[10000001];
//bool compar(loto nr1,loto nr2)
//{
//return nr1.suma<nr2.suma;
//}
int compar(loto a,loto b)
{
if(a.suma<b.suma)
return 1;
else return 0;
}
int caut(int s,int d,int x)
{
if(s>d)
return -1;
else
{
int m=(s+d)/2;
if(x == v[m].suma)
return m;
if(x < v[m].suma)
return caut(s,m-1,x);
if(x > v[m].suma)
return caut(m+1,d,x);
}
}
int main()
{
f>>n;
f>>s;
int nr=0;
for(i=1;i<=n;i++)
f>>vect[i];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
for(o=j;o<=n;o++)
{
v[nr].suma=vect[i]+vect[j]+vect[o];
v[nr].x=vect[i];
v[nr].y=vect[j];
v[nr].z=vect[o];
nr++;
}
sort(v,v+nr,compar);
for(i=0;i<nr;i++)
{
ok=0;
bilet=0;
val=s-v[i].suma;
if(caut(0,nr-1,val)!=-1)
{bilet=val;
poz=caut(0,nr-1,val);
}
if(v[i].suma+bilet == s)
{out<<v[i].x<<" "<<v[i].y<<" "<<v[i].z<<" ";
out<<v[poz].x<<" "<<v[poz].y<<" "<<v[poz].z<<" ";
//cout<<v[i].suma<<" "<<bilet;
ok=1;
break;
}
}
if(ok==0)
out<<-1;
// for(i=0;i<nr;i++)
// cout<<v[i].suma;
return 0;
}