diff --git a/ex1/ex1.c b/ex1/ex1.c index 77716b62e947476aa565e18190bd78ec359362eb..09e8111c80dfe457256897079f4e34f6dbcc1408 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -5,5 +5,19 @@ int main(int argc, char **argv) { + bool first = true; + int decnum = 0; + int dectot = 0; + int prev, next; + scanf("%d", &prev); + for (int i = 1; i < 8; ++i) { + scanf("%d", &next); + if (next < prev) { + ++decnum; + dectot += (prev - next); + } + prev = next; + } + printf("\n%d %d\n\n", decnum, dectot); return EXIT_SUCCESS; } diff --git a/ex2/ex2.c b/ex2/ex2.c index 77716b62e947476aa565e18190bd78ec359362eb..81687c243d9240c1943ca6011c5f4523eb18b3ee 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -4,6 +4,110 @@ #include <stdlib.h> +#define INSERT_VALUE 0 + + +typedef struct _link { + int value; + struct _link *next; +} elem; + +typedef struct _list { + elem *head; +} list; + +list list_create() { + list ll; + ll.head = NULL; + return ll; +} + +void list_destroy(list* ll) { + elem *e = ll->head; + while (ll->head != NULL) { + ll->head = ll->head->next; + free(e); + e = ll->head; + } +} + +void list_push(list* ll, const int v) { + elem *e = malloc(sizeof(elem)); + e->value = v; + e->next = ll->head; + ll->head = e; +} + +void list_insert(list* ll, const int n, const int v) { + if (n < 0) { return; } + elem *e = malloc(sizeof(elem)); + e->value = v; + e->next = NULL; + if (0 == n) { + e->next = ll->head; + ll->head = e; + } else { + int count = 1; + elem *prev = ll->head; + while (count < n) { + if (NULL == prev->next) { return; } + prev = prev->next; + ++count; + } + elem *follow = prev->next; + e->next = follow; + prev->next = e; + } +} + +void list_remove(list *ll, const int n) { + if (n < 0) { return; } + if (0 == n) { + elem *e = ll->head; + ll->head = ll->head->next; + free(e); + } else { + int count = 1; + elem *prev = ll->head; + elem *e = prev->next; + while (count < n) { + if (NULL == e) { return; } + prev = e; + e = e->next; + ++count; + } + if (e != NULL) { prev->next = e->next; } + else { prev->next = NULL; } + free(e); + } +} + +void list_print(const list *ll) { + elem *e = ll->head; + while (e != NULL) { + printf("%d", e->value); + if (e->next != NULL) { printf(" "); } + e = e->next; + } + printf("\n"); +} + + int main(int argc, char **argv) { + int n; + list ll = list_create(); + for (int i = 0; i < 4; ++i) { + scanf("%d", &n); + list_push(&ll, n); + } + int i, r; + scanf("%d", &i); + scanf("%d", &r); + printf("\n"); + list_insert(&ll, i, INSERT_VALUE); + list_print(&ll); + list_remove(&ll, r); + list_print(&ll); + list_destroy(&ll); return EXIT_SUCCESS; } diff --git a/ex3/ex3.c b/ex3/ex3.c index 77716b62e947476aa565e18190bd78ec359362eb..df55467f72fe89c8ad551477fb68137fe08b98aa 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -3,7 +3,115 @@ #include <stdint.h> #include <stdlib.h> +typedef struct _link { + int value; + struct _link *next; +} elem; + +typedef struct _list { + elem *head; +} list; + +list list_create() { + list ll; + ll.head = NULL; + return ll; +} + +void list_destroy(list* ll) { + elem *e = ll->head; + while (ll->head != NULL) { + ll->head = ll->head->next; + free(e); + e = ll->head; + } +} + +void list_push(list* ll, const int v) { + elem *e = malloc(sizeof(elem)); + e->value = v; + e->next = ll->head; + ll->head = e; +} + +void list_insert(list* ll, const int n, const int v) { + if (n < 0) { return; } + elem *e = malloc(sizeof(elem)); + e->value = v; + e->next = NULL; + if (0 == n) { + e->next = ll->head; + ll->head = e; + } else { + int count = 1; + elem *prev = ll->head; + while (count < n) { + if (NULL == prev->next) { return; } + prev = prev->next; + ++count; + } + elem *follow = prev->next; + e->next = follow; + prev->next = e; + } +} + +void list_remove(list *ll, const int n) { + if (n < 0) { return; } + if (0 == n) { + elem *e = ll->head; + ll->head = ll->head->next; + free(e); + } else { + int count = 1; + elem *prev = ll->head; + elem *e = prev->next; + while (count < n) { + if (NULL == e) { return; } + prev = e; + e = e->next; + ++count; + } + if (e != NULL) { prev->next = e->next; } + else { prev->next = NULL; } + free(e); + } +} + +void list_print(const list *ll) { + elem *e = ll->head; + while (e != NULL) { + printf("%d", e->value); + if (e->next != NULL) { printf(" "); } + e = e->next; + } + printf("\n"); +} + +// ^^^^ repris de la question 2 + + +list pascal(const int line) { + if (line <= 1) { + list ll = list_create(); + ll.push(1); + return ll; + } else { + list above = pascal(line-1); // rec + elem *e = above.head; + list_ll = list_create(); + list_destroy(&above); + return ll; + } +} + int main(int argc, char **argv) { + int line; + scanf("%d", &line); + printf("\n"); + list ll = pascal(line); + list_print(&ll); + list_destroy(&ll); return EXIT_SUCCESS; } diff --git a/ex4/ex4.c b/ex4/ex4.c index 77716b62e947476aa565e18190bd78ec359362eb..e1b0077ec39ecff3e0fa3bdc7728eceb50054192 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -5,5 +5,26 @@ int main(int argc, char **argv) { + size_t size = 0; + scanf("%lud", &size); + int px[size]; + int py[size]; + for (size_t i = 0; i < size; ++i) { + scanf("%d %d", &(px[i]), &(py[i])); + } + int matrix[size][size]; + for (size_t i = 0; i < size; ++i) { + for (size_t k = 0; k < size; ++k) { + matrix[i][k] = abs(px[i] - px[k]) + abs(py[i] - py[k]); + } + } + printf("\n"); + for (size_t i = 0; i < size; ++i) { + for (size_t k = 0; k < size; ++k) { + printf("%d", matrix[i][k]); + if (k != (k-1)) { printf(" "); } + } + printf("\n"); + } return EXIT_SUCCESS; }