Cod sursa(job #1697534)

Utilizator andreicoman299Coman Andrei andreicoman299 Data 2 mai 2016 13:16:17
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <stdio.h>
#include <stdlib.h>
#define MAXN 100

int v[MAXN], c3[MAXN*MAXN*MAXN][4];
void myqsort(int begin, int end){
    int b=begin, e=end, pivot=c3[(b+e)/2][0];
    while(b<=e){
        while(c3[b][0]<pivot) b++;
        while(c3[e][0]>pivot) e--;
        if(b<=e){
            int i;
            for(i=0;i<4;i++){
                int aux=c3[b][i];
                c3[b][i]=c3[e][i];
                c3[e][i]=aux;
            }
            b++; e--;
        }
    }
    if(b<end) myqsort(b, end);
    if(begin<e) myqsort(begin, e);
}

int main(){
    int n, s, i, j, k;
    FILE*fi,*fo;
    fi=fopen("loto.in","r");
    fo=fopen("loto.out","w");
    fscanf(fi,"%d%d", &n, &s);
    for(i=0;i<n;i++)
        fscanf(fi,"%d", &v[i]);
    int ind=0;
    for(i=0;i<n;i++)
        for(j=i;j<n;j++)
            for(k=j;k<n;k++){
                c3[ind][0]=v[i]+v[j]+v[k];
                c3[ind][1]=v[i];
                c3[ind][2]=v[j];
                c3[ind++][3]=v[k];
            }
    myqsort(0, ind-1);
    j=ind-1;
    for(i=0;i<ind;i++){
        int diff=s-c3[i][0];
        while(j>=0 && c3[j][0]>diff)
            j--;
        if(j>=0 && c3[j][0]==diff){
            fprintf(fo,"%d %d %d %d %d %d", c3[i][1], c3[i][2], c3[i][3], c3[j][1], c3[j][2], c3[j][3]);
            return 0;
        }
    }
    fprintf(fo,"-1");
    fclose(fi);
    fclose(fo);
    return 0;
}