From cf87e5f67ae18767b933edb3b1371696f6b1c582 Mon Sep 17 00:00:00 2001 From: Matthew Sotoudeh Date: Fri, 7 Oct 2022 12:46:02 -0700 Subject: algorithms --- alg_r.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 alg_r.c (limited to 'alg_r.c') diff --git a/alg_r.c b/alg_r.c new file mode 100644 index 0000000..755fb65 --- /dev/null +++ b/alg_r.c @@ -0,0 +1,65 @@ +#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(1 < t && t < n); + + size_t *c = calloc(t + 2, sizeof(*c)); + size_t j; + +R1_initialize: + for (size_t j = 1; j <= t; j++) + c[j] = j - 1; + c[t + 1] = n; + +R2_visit: + for (size_t k = t; k >= 1; k--) + printf("%lu ", c[k]); + printf("\n"); + +R3_maybe_easy_case: + if ((t % 2) == 1) { + if (c[1] + 1 < c[2]) { + c[1] = c[1] + 1; + goto R2_visit; + } + j = 2; + goto R4_try_decrease_c_j; + } else { + if (c[1] > 0) { + c[1] = c[1] - 1; + goto R2_visit; + } else { + j = 2; + goto R5_try_increase_c_j; + } + } + +R4_try_decrease_c_j: + if (c[j] >= j) { + c[j] = c[j - 1]; + c[j - 1] = j - 2; + goto R2_visit; + } + j = j + 1; + +R5_try_increase_c_j: + if (c[j] + 1 < c[j + 1]) { + c[j - 1] = c[j]; + c[j] = c[j] + 1; + goto R2_visit; + } else { + j = j + 1; + if (j <= t) goto R4_try_decrease_c_j; + } +} -- cgit v1.2.3