Pagini recente » Cod sursa (job #1674293) | Cod sursa (job #2375872) | Cod sursa (job #1833517) | Cod sursa (job #614554) | Cod sursa (job #2591646)
#include <stdlib.h>
#include <stdio.h>
#define MOD 1000003
typedef struct triplet {
int a, b, c;
} t_triplet;
typedef struct cell {
t_triplet value;
struct cell *next;
} t_cell;
typedef struct list {
t_cell *start, *end;
} t_list;
typedef t_list* t_hash;
typedef int (* f_hash) (int);
t_list *create_list();
void free_list(t_list *list);
int push_back_list(t_list **list, t_triplet value);
int remove_list(t_list *list, t_triplet value);
t_triplet *get_list(t_list *list, int sum);
int add_hash(t_hash *hash, f_hash f, t_triplet value);
t_triplet *get_hash(t_hash *hash, f_hash f, int sum);
t_list *create_list()
{
t_list *list;
list = (t_list *) malloc(sizeof(t_list));
if(list == NULL) {
return NULL;
}
list->start = list->end = NULL;
return list;
}
void free_list(t_list *list)
{
if(list == NULL) {
return;
}
for(t_cell *cell = list->start, *next = NULL; cell != NULL; cell = next) {
next = cell->next;
free(cell);
}
}
int push_back_list(t_list **list, t_triplet value)
{
t_cell *cell;
if(*list == NULL) {
*list = create_list();
if(*list == NULL) {
return -1;
}
}
cell = (t_cell *) malloc(sizeof(t_cell));
if(cell == NULL) {
return -1;
}
cell->value.a = value.a;
cell->value.b = value.b;
cell->value.c = value.c;
cell->next = NULL;
if((*list)->end == NULL) {
(*list)->start = (*list)->end = cell;
}
else {
(*list)->end->next = cell;
(*list)->end = cell;
}
return 0;
}
t_triplet *get_list(t_list *list, int sum) {
if(list == NULL) {
return NULL;
}
for(t_cell *cell = list->start; cell != NULL; cell = cell->next) {
if(cell->value.a + cell->value.b + cell->value.c == sum) {
return &cell->value;
}
}
return NULL;
}
int add_hash(t_hash *hash, f_hash f, t_triplet value)
{
int p;
p = f(value.a + value.b + value.c);
return push_back_list(&(hash[p]), value);
}
t_triplet *get_hash(t_hash *hash, f_hash f, int sum)
{
int p;
p = f(sum);
return get_list(hash[p], sum);
}
int modulo(int value)
{
return value % MOD;
}
t_hash h[MOD];
int main()
{
FILE *fin = fopen("loto.in", "r"),
*fout = fopen("loto.out", "w");
int n, s, i, j, k, found = 0, v[101];
t_triplet value;
t_triplet *res;
fscanf(fin, "%d %d", &n, &s);
for(i = 0; i < n; i++) {
fscanf(fin,"%d", &v[i]);
}
for(i = 0; i < n; i++) {
value.a = v[i];
for(j = 0; j < n; j++) {
value.b = v[j];
for(k = 0; k < n; k++) {
value.c = v[k];
add_hash(h, &modulo, value);
}
}
}
for(i = 0; i < n && !found; i++) {
for(j = 0; j < n && !found; j++) {
for(k = 0; k < n && !found; k++) {
if((res = get_hash(h, &modulo, s - (v[i] + v[j] + v[k])))) {
fprintf(fout, "%d %d %d %d %d %d",
v[i], v[j], v[k], res->a, res->b, res->c);
found = 1;
}
}
}
}
if(!found) {
fprintf(fout, "-1");
}
fclose(fin);
fclose(fout);
}