skomb/src/skomb.h

223 lines
5.2 KiB
C

#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); /* read len ammount of characters from stdin into line */
int splitc_toi(char *line, int sep, int *dest); /* split a char array of numbers into an int array, separator used between numbes must be provided in the function call (e.g. commas, spaces, etc.) */
int skomb2(int number, int *not_allowed, int* not_allowed_len, pair *pairs); /* stores valid combinations that sum up to number respecting an int array of not allowed numbers with it's length in a pair array */
int skomb3(int number, int *not_allowed, int* not_allowed_len, trio *trios); /* same as above but with trios instead */
int skomb4(int number, int *not_allowed, int* not_allowed_len, quartet *quartets); /* same as above but with quartets instead */
int skomb5(int number, int *not_allowed, int* not_allowed_len, quintet *quintets); /* same as above but with quintets instead */
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 skomb2(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 skomb3(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 = skomb2(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 skomb4(int number, int *not_allowed, int *not_allowed_len, quartet *quartets){
int i, j, k;
int skip, len;
k = 0;
trio *trios = malloc(sizeof(trio) * 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 = skomb3(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 skomb5(int number, int *not_allowed, int* not_allowed_len, quintet *quintets){
int i, j, k;
int skip, len;
k = 0;
quartet *quartets = malloc(sizeof(quartet) * 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 = skomb4(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