From cf87e5f67ae18767b933edb3b1371696f6b1c582 Mon Sep 17 00:00:00 2001 From: Matthew Sotoudeh Date: Fri, 7 Oct 2022 12:46:02 -0700 Subject: algorithms --- alg_t.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 alg_t.c (limited to 'alg_t.c') diff --git a/alg_t.c b/alg_t.c new file mode 100644 index 0000000..9607672 --- /dev/null +++ b/alg_t.c @@ -0,0 +1,61 @@ +#include +#include +#include + +int main(int argv, char **argc) { + if (argv < 3) { + assert(argv > 0); + printf("Usage: %s [n] [t]\n", argc[0]); + return -1; + } + + size_t n = atoi(argc[1]), + t = atoi(argc[2]); + + assert(t < n); + + size_t *c = calloc(t + 3, sizeof(*c)); + +T1_initialize: + for (size_t j = 1; j <= t; j++) + c[j] = j - 1; + c[t + 1] = n; + c[t + 2] = 0; + + size_t j = t; + +T2_visit: + for (size_t k = t; k >= 1; k--) + printf("%lu ", c[k]); + printf("\n"); + + size_t x; + if (j > 0) { + x = j; + goto T6_increase_c_j; + } + +T3_maybe_easy_case: + if (c[1] + 1 < c[2]) { + c[1] = c[1] + 1; + goto T2_visit; + } else { + j = 2; + } + +T4_find_j: + c[j - 1] = j - 2; + x = c[j] + 1; + if (x == c[j + 1]) { + j = j + 1; + goto T4_find_j; + } + +T5_maybe_done: + if (j > t) return 0; + +T6_increase_c_j: + c[j] = x; + j = j - 1; + goto T2_visit; +} -- cgit v1.2.3