skomb/src/skomb.h

223 lines
4.7 KiB
C
Raw Normal View History

2023-01-04 17:14:28 -03:00
#ifndef STDIO_H
#define STDIO_H
#include <stdio.h>
#endif
#ifndef STRING_H
#define STRING_H
#include <string.h>
#endif
#ifndef STDLIB_H
#define STDLIB_H
#include <stdlib.h>
#endif
#ifndef SKOMBLIB_H
#define SKOMBLIB_H
typedef struct pair{
int x;
int y;
} pair;
typedef struct trio{
int x;
int y;
int z;
} trio;
typedef struct quartet{
int x;
int y;
int z;
int w;
} quartet;
typedef struct quintet{
int x;
int y;
int z;
int w;
int v;
} quintet;
void readline(char *line, int len);
int splitc_toi(char *line, int sep, int *dest);
int perm2(int number, int *not_allowed, int* not_allowed_len, pair *pairs);
int perm3(int number, int *not_allowed, int* not_allowed_len, trio *trios);
int perm4(int number, int *not_allowed, int* not_allowed_len, quartet *quartets);
int perm5(int number, int *not_allowed, int* not_allowed_len, quintet *quintets);
void readline(char *line, int len){
int i = 0;
int c;
while ((c = getchar()) != '\n' && i < len){
line[i] = c;
i++;
}
line[i] = '\0';
}
int splitc_toi(char *line, int sep, int *dest){
int i, j;
i = j = 0;
char temp[2];
temp[1] = '\0';
while (line[i] != '\0'){
if (line[i] == sep){
i++;
continue;
}
temp[0] = line[i];
dest[j] = atoi(temp);
i++;
j++;
}
return j;
}
int perm2(int number, int* not_allowed, int* not_allowed_len, pair* dest){
int i, j, k;
int skip;
k = 0;
for (i = 1; i < 10; i++){
for (j = 0; j < *not_allowed_len; j++){
if (i == not_allowed[j] || number - i == not_allowed[j]){
skip = 1;
break;
}
}
if (skip == 1){
skip = 0;
continue;
}
if (i != number && i < number - i && number - i < 10){
dest[k].x = i;
dest[k].y = number - i;
k++;
}
}
return k;
}
int perm3(int number, int *not_allowed, int *not_allowed_len, trio *trios){
int i, j, k;
int skip, len;
k = 0;
pair *pairs = malloc(sizeof(pair) * 4);
for (i = 1; i < 10; i++){
for (j = 0; j < *not_allowed_len; j++){
if (i == not_allowed[j]){
skip = 1;
break;
}
}
if (skip == 1){
skip = 0;
continue;
}
if (i != number && number - i < 18 && number - i > 2
&& i < number){
not_allowed[*not_allowed_len] = i;
(*not_allowed_len)++;
len = perm2(number-i, not_allowed, not_allowed_len, pairs);
if (len > 0){
for (j = 0; j < len; j++){
if (i < pairs[j].x && i < pairs[j].y){
trios[k].x = i;
trios[k].y = pairs[j].x;
trios[k].z = pairs[j].y;
k++;
}
}
}
(*not_allowed_len)--;
not_allowed[*not_allowed_len] = 0;
}
}
free(pairs);
return k;
}
int perm4(int number, int *not_allowed, int *not_allowed_len, quartet *quartets){
int i, j, k;
int skip, len;
k = 0;
trio *trios = malloc(sizeof(pair) * 8);
for (i = 1; i < 10; i++){
for (j = 0; j < *not_allowed_len; j++){
if (i == not_allowed[j]){
skip = 1;
break;
}
}
if (skip == 1){
skip = 0;
continue;
}
if (i != number && number - i < 25 && number - i > 5){
not_allowed[*not_allowed_len] = i;
(*not_allowed_len)++;
len = perm3(number - i, not_allowed, not_allowed_len, trios);
if (len > 0){
for (j = 0; j < len; j++){
if (i < trios[j].x && i < trios[j].y && i < trios[j].z){
quartets[k].x = i;
quartets[k].y = trios[j].x;
quartets[k].z = trios[j].y;
quartets[k].w = trios[j].z;
k++;
}
}
}
(*not_allowed_len)--;
not_allowed[*not_allowed_len] = 0;
}
}
free(trios);
return k;
}
int perm5(int number, int *not_allowed, int* not_allowed_len, quintet *quintets){
int i, j, k;
int skip, len;
k = 0;
quartet *quartets = malloc(sizeof(pair) * 12);
for (i = 1; i < 10; i++){
for (j = 0; j < *not_allowed_len; j++){
if (i == not_allowed[j]){
skip = 1;
break;
}
}
if (skip == 1){
skip = 0;
continue;
}
if (i != number && number - i < 31 && number - i > 9){
not_allowed[*not_allowed_len] = i;
(*not_allowed_len)++;
len = perm4(number-i, not_allowed, not_allowed_len, quartets);
if (len > 0){
for (j = 0; j < len; j++){
if (i < quartets[j].x && i < quartets[j].y && i < quartets[j].z && i < quartets[j].w){
quintets[k].x = i;
quintets[k].y = quartets[j].x;
quintets[k].z = quartets[j].y;
quintets[k].w = quartets[j].z;
quintets[k].v = quartets[j].w;
k++;
}
}
}
(*not_allowed_len)--;
not_allowed[*not_allowed_len] = 0;
}
}
free(quartets);
return k;
}
#endif