diff options
| -rw-r--r-- | challenge-013/paulo-custodio/c/ch-1.c | 64 | ||||
| -rw-r--r-- | challenge-013/paulo-custodio/c/ch-2.c | 52 |
2 files changed, 116 insertions, 0 deletions
diff --git a/challenge-013/paulo-custodio/c/ch-1.c b/challenge-013/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..d15a9dfd1c --- /dev/null +++ b/challenge-013/paulo-custodio/c/ch-1.c @@ -0,0 +1,64 @@ +/* +Challenge 013 + +Challenge #1 +Write a script to print the date of last Friday of every month of a given year. +For example, if the given year is 2019 then it should print the following: + +2019/01/25 +2019/02/22 +2019/03/29 +2019/04/26 +2019/05/31 +2019/06/28 +2019/07/26 +2019/08/30 +2019/09/27 +2019/10/25 +2019/11/29 +2019/12/27 +*/ + +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> + +enum { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; + +int day_of_week(int y, int m, int d) { + return (d += m < 3 ? y-- : y - 2, 23*m/9 + d + 4 + y/4- y/100 + y/400)%7; +} + +bool is_leap_year(int y) { + return y%400==0 ? true : y%100==0 ? false : y%4==0 ? true : false; +} + +int days_of_month(int y, int m) { + switch (m) { + case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; + case 4: case 6: case 9: case 11: return 30; + case 2: return is_leap_year(y) ? 29 : 28; + default: assert(0); + } +} + +void last_friday_month(int y, int m, int* d) { + *d = days_of_month(y, m); + while (day_of_week(y, m, *d) != Friday) + (*d)--; +} + +int main(int argc, char* argv[]) { + if (argc != 2) { + fputs("usage: ch-1 year", stderr); + return EXIT_FAILURE; + } + + int y = atoi(argv[1]); + for (int m = 1; m <= 12; m++) { + int d; + last_friday_month(y, m, &d); + printf("%04d/%02d/%02d\n", y, m, d); + } +} diff --git a/challenge-013/paulo-custodio/c/ch-2.c b/challenge-013/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..1cf93500af --- /dev/null +++ b/challenge-013/paulo-custodio/c/ch-2.c @@ -0,0 +1,52 @@ +/* +Challenge 013 + +Challenge #2 +Write a script to demonstrate Mutually Recursive methods. Two methods are +mutually recursive if the first method calls the second and the second calls +first in turn. Using the mutually recursive methods, generate Hofstadter +Female and Male sequences. + + F ( 0 ) = 1 ; M ( 0 ) = 0 + F ( n ) = n − M ( F ( n − 1 ) ) , n > 0 + M ( n ) = n − F ( M ( n − 1 ) ) , n > 0. +*/ + +#include <stdio.h> +#include <stdlib.h> + +int M(int n); + +int F(int n) { + if (n == 0) return 1; + return n - M( F(n - 1) ); +} + +int M(int n) { + if (n == 0) return 0; + return n - F( M(n - 1) ); +} + +int main(int argc, char* argv[]) { + if (argc != 2) { + fputs("usage: ch-1 N", stderr); + return EXIT_FAILURE; + } + + int N = atoi(argv[1]); + const char* sep = ""; + printf("F: "); + for (int n = 0; n < N; n++) { + printf("%s%d", sep, F(n)); + sep = ", "; + } + printf(", ...\n"); + + sep = ""; + printf("M: "); + for (int n = 0; n < N; n++) { + printf("%s%d", sep, M(n)); + sep = ", "; + } + printf(", ...\n"); +} |
