Pagini recente » Cod sursa (job #1402917) | Cod sursa (job #2537156) | Cod sursa (job #703013) | Cod sursa (job #833527) | Cod sursa (job #523235)
Cod sursa(job #523235)
#include <cstdio>
#include <cstdlib>
#define MAXN 101
struct sum {
int val;
int a, b, c;
};
int compare(const void *a, const void *b){
return (((sum*)a)->val-((sum*)b)->val);
}
int main(){
freopen("loto.in", "r", stdin);
freopen("loto.out", "w", stdout);
int N, S, loto[MAXN], spos, found=0;
int left, right, mid, key;
int i, j, k;
static sum V[MAXN*MAXN*MAXN];
scanf("%d%d", &N, &S);
for(i=0; i<N; i++)
scanf("%d", loto+i);
spos=0;
for(i=0; i<N; i++)
for(j=i; j<N; j++)
for(k=j; k<N; k++){
V[spos].val=loto[i]+loto[j]+loto[k];
V[spos].a=loto[i]; V[spos].b=loto[j]; V[spos].c=loto[k];
spos++;
}
qsort(V, spos, sizeof(sum), compare);
for(i=0; i<spos && !found; i++){
key=S-V[i].val; left=0; right=spos-1;
while(left <= right){
mid=((right-left)>>1)+left;
if(key == V[mid].val){
printf("%d %d %d %d %d %d\n", V[i].a, V[i].b, V[i].c, V[mid].a, V[mid].b, V[mid].c);
found=1;
break;
} else if(key<V[mid].val){
right=mid-1;
} else {
left=mid+1;
}
}
}
if(!found)
printf("%d\n", -1);
return 0;
}