Cod sursa(job #611537)

Utilizator badmanDragan Dan badman Data 1 septembrie 2011 21:20:57
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <fstream>
#include <algorithm>
#define max 1000000

using namespace std;
typedef struct {int a,b,c,suma;} triplet;
triplet c[max];          
int a[101],n,s;

bool comparare(triplet a,triplet b)
{
   return a.suma>b.suma;  
}

int main(void){
    int i,j,k,nr;
    ifstream f("loto.in");
    ofstream g("loto.out");
    f>>n>>s; nr=0;

    for(i=1;i<=n;i++) f>>a[i]; f.close();
    for(i=1;i<=n;i++)
      for(j=i;j<=n;j++)
        for(k=j;k<=n;k++)
            c[++nr].suma=(c[nr].a=a[i])+(c[nr].b=a[j])+(c[nr].c=a[k]); 
    sort(c+1,c+nr+1,comparare);        
    i=1; j=nr;       
    while((c[i].suma+c[j].suma!=s) && (i<=j))
                              if(c[i].suma+c[j].suma<s) --j;
                                 else ++i; 
    if (i<=j) g<<c[i].a<<" "<<c[i].b<<" "<<c[i].c<<" "<<c[j].a<<" "<<c[j].b<<" "<<c[j].c<<" ";
       else g<<-1;  
                                                   
    g.close();
 return 0;   
}