Pagini recente » Cod sursa (job #916030) | Cod sursa (job #743000)
Cod sursa(job #743000)
#include <fstream>
#include <algorithm>
#define nmax 105
#define combmax 1000005
using namespace std;
ifstream f("loto.in");
ofstream g("loto.out");
int n, s, a[nmax], cnt, poz;
typedef struct{
int a, b, c, nr;
}camp;
camp comb[combmax];
void citeste(){
f >> n >> s;
for(int i=1; i<=n; i++) f >> a[i];
}
void prec(){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
for(int k=1; k<=n; k++){
int sum = a[i] + a[j] + a[k];
camp val;
val.a = a[i];
val.b = a[j];
val.c = a[k];
val.nr = sum;
comb[++cnt] = val;
}
}
}
}
bool cb(int x){
int st = 1;
int dr = cnt;
while (st <= dr){
int mij = (st + dr) / 2;
if (comb[mij].nr == x){
poz = mij;
return 1;
}else if(comb[mij].nr < x ){
st = mij + 1;
}else if(comb[mij].nr > x ){
dr = mij - 1;
}
}
return 0;
}
void rezolva(){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
for(int k=1; k<=n; k++){
int sum = s - (a[i] + a[j] + a[k]);
poz = 0;
if (cb(sum)){
g << a[i] << " " << a[j] << " " << a[k] << " " << comb[poz].a << " " << comb[poz].b << " " << comb[poz].c << "\n";
return;
}
}
}
}
}
int main(){
citeste();
prec();
rezolva();
f.close();
g.close();
return 0;
}