Cod sursa(job #1222365)

Utilizator g3ppyStoian Vlad g3ppy Data 22 august 2014 22:53:18
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.94 kb
#include <stdio.h>

#define NMAX 1001
#define HASH_SIZE 44497

using namespace std;

struct three {
    three(int zero, int one, int two) {
        this->zero = zero;
        this->one = one;
        this->two = two;
    }

    three() {
        this->zero = 0;
        this->one = 0;
        this->two = 0;
    }

    int get_sum() {
        int sum = 0;
        sum += zero + one + two;
        return sum;
    }

    int zero, one, two;
};

int numbers[NMAX];


struct hash_node {
    int key;
    three value;
    hash_node *next;
};

hash_node* hash_table[HASH_SIZE] = { NULL };

hash_node* get(int key) {
    hash_node *element = hash_table[key % HASH_SIZE];

    while(element != NULL && element->key != key) {
        element = element->next;
    }

    return element;
}

void put(int key, three value) {
    if (get(key) != NULL) return;

    hash_node *element = hash_table[key % HASH_SIZE];
    hash_node *new_element = new hash_node;
    new_element->key = key;
    new_element->value = value;
    new_element->next = element;

    hash_table[key % HASH_SIZE] = new_element;
}


int main() {
    freopen("loto.in", "rt", stdin);
    freopen("loto.out", "wt", stdout);


    hash_node *c;

    c->next;

    int n, s;
    scanf("%d %d\n", &n, &s);


    for (int i = 0; i < n; i++) {
        scanf("%d", &numbers[i]);
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < n; k++) {
                three element(numbers[i], numbers[j], numbers[k]);
                put(element.get_sum(), element);

                if (s < element.get_sum()) continue;

                hash_node* other = get(s - element.get_sum());

                if (other != NULL) {
                    printf("%d %d %d %d %d %d\n", other->value.zero, other->value.one, other->value.two, element.zero, element.one, element.two);
                    return 0;
                }
            }
        }
    }

    printf("%d\n", -1);

    return 0;
}