Cod sursa(job #1719989)

Utilizator stefanik_robertzRobert S stefanik_robertz Data 20 iunie 2016 20:37:55
Problema Loto Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <iostream>
#include <fstream>
using namespace std;
int n;
long s,val[100],sol[100];
bool cauta(int x,int ci,int cf){
    if(ci<=cf){
        int m=(ci+cf)/2;
        if(val[m]==x)return true;
        if(x<val[m])return cauta(x,ci,m);
        return cauta(x,m+1,cf);
    }
    return false;
}
bool loto(int cnt){
    if(cnt==0&&s==0)return true;
    if(s>0){

        int aux;
        aux=s%cnt;
        if(aux==0)aux=s/cnt;
        {
            while(aux<val[n-1]){
                if(cauta(aux,0,n-1))break;
                aux++;
            }
            if(aux>val[n-1])return false;
            sol[cnt]=aux;
            s-=aux;
        }
        return loto(cnt-1);
    }
    return false;
}
int main()
{
    ifstream f("loto.in");
    ofstream g("loto.out");
    f>>n>>s;
    for(int i=0;i<n;i++){
        f>>val[i];
    }
    if(loto(6)){
        for(int i=6;i>=1;i--)cout<<sol[i]<<" ";
    }else cout<<-1;
    return 0;
}