Pagini recente » Cod sursa (job #1027108) | Cod sursa (job #1013951) | Cod sursa (job #819590) | Cod sursa (job #2480473) | Cod sursa (job #675460)
Cod sursa(job #675460)
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
int a[1000001];
int v[101],l;
int cautarebinara(int x)
{
int p,q,mij;
p=1;
q=l;
while(p<=q) {
mij=(p+q)/2;
if(a[mij]>x)
q=mij-1;
else if(a[mij]<x)
p=mij+1;
else
return 1;
}
return 0;
}
int main ()
{
int n,i,j,k,sum,d,s,c;
ifstream f("loto.in");
ofstream g("loto.out");
f>>n>>sum;
for(i=1;i<=n;i++)
f>>v[i];
f.close();
l=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
for(k=1;k<=n;k++)
if((v[i]+v[j]+v[k])<=sum) {
l++;
a[l]=v[i]+v[j]+v[k];
}
sort(a+1,a+l+1);
d=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
for(k=1;k<=n;k++) {
s=v[i]+v[j]+v[k];
if(cautarebinara(sum-s)==1) {
g<<v[i]<<" "<<v[j]<<" "<<v[k]<<" ";
c=s;
i=n+1;
j=n+1;
k=n+1;
d=1;
break;
}
}
if(d==0)
g<<"-1";
else {
sum=sum-c;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
for(k=1;k<=n;k++) {
s=v[i]+v[j]+v[k];
if(s==sum) {
g<<v[i]<<" "<<v[j]<<" "<<v[j];
i=n+1;
j=n+1;
k=n+1;
break;
}
}
}
g.close();
return 0;
}