Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • algorithmique/cours
  • aurelien.boyer/cours
  • jeremy.meissner/cours
  • radhwan.hassine/cours
  • yassin.elhakoun/cours-algo
  • gaspard.legouic/cours
  • joachim.bach/cours
  • gabriel.marinoja/algo-cours
  • loic.lavorel/cours
  • iliya.saroukha/cours
  • costanti.volta/cours
  • jacquesw.ndoumben/cours
12 results
Select Git revision
Show changes
Showing
with 697 additions and 0 deletions
#include <stdio.h>
int pgcd(int m, int n) {
if (m % n == 0) {
return n;
} else {
return pgcd(n, m % n);
}
}
// int pgcd(int m, int n) {
// if (m % n == 0) {
// return n;
// } else {
// return pgcd(n, m % n);
// }
// }
int main() {
int m = 42;
int n = 27;
printf("Le PGCD de %d et %d est %d\n", m, n, pgcd(m, n));
}
#include <stdio.h>
double pow_naive(double x, int n) {
if (n == 0) {
return 1.0;
}
double res = x;
for (int i = 1; i < n; i++) {
res *= x;
}
return res;
}
double pow_rec(double x, int n) {
if (n == 0) {
return 1.0;
} else {
return x * pow_rec(x, n - 1);
}
}
double pow_eff(double x, int n) {
if (n == 0) {
return 1.0;
} else if (n % 2 == 0) {
double intermediate = pow_eff(x, n / 2);
return intermediate * intermediate;
} else {
return x * pow_eff(x, n - 1);
}
}
int main() {
double x = 2;
int n = 10000;
double y = 0;
for (int i = 0; i < 1000; ++i) {
// y += pow_eff(x, n);
y += pow_naive(x, n);
}
// printf("%lf^%d = %lf\n", x, n, pow_naive(x, n));
// printf("%d^%d = %lf\n", x, n, pow_rec(x, n));
// printf("%lf^%d = %lf\n", x, n, pow_eff(x, n));
printf("y = %lf\n", y);
}
#include <stdio.h>
int ppcm(int m, int n) {
int tmp_m = m;
int tmp_n = n;
while (m != n) {
if (m < n) {
m += tmp_m;
} else {
n += tmp_n;
}
}
return m;
}
int main() {
int m = 36;
int n = 90;
printf("the ppcm is %d\n", ppcm(m, n));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define NX 8
#define NY 8
int main() {
char tab[NX][NY];
int cx = 4;
int cy = 7;
for (int i = 0; i < NX; ++i) {
for (int j = 0; j < NY; ++j) {
if (i == cy || j == cx) {
tab[i][j] = '*';
} else if (abs(cy - i) == abs(cx - j)) {
tab[i][j] = '*';
} else {
tab[i][j] = ' ';
}
}
}
tab[cy][cx] = 'R';
for (int i = 0; i < NX; ++i) {
for (int j = 0; j < NY; ++j) {
printf("%c ", tab[i][j]);
}
printf("\n");
}
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
struct element {
int data;
struct element *next;
};
struct queue {
struct element *head;
struct element *tail;
};
void queue_init(struct queue *fa) {
fa->head = NULL;
fa->tail = NULL;
}
bool queue_is_empty(struct queue fa) {
return (fa.head == NULL && fa.tail == NULL);
}
int queue_tail(struct queue fa) {
assert(!queue_is_empty(fa) && "Careful the queue is empty.");
return fa.tail->data;
}
int queue_head(struct queue fa) {
assert(!queue_is_empty(fa) && "Careful the queue is empty.");
return fa.head->data;
}
void queue_enqueue(struct queue *fa, int val) {
struct element *elem = malloc(sizeof(*elem));
elem->data = val;
elem->next = NULL;
if (queue_is_empty(*fa)) {
fa->tail = elem;
fa->head = elem;
} else {
fa->tail->next = elem;
fa->tail = elem;
}
}
int queue_dequeue(struct queue *fa) {
if (queue_is_empty(*fa)) {
assert(false && "Empty queue, impossible to dequeue.");
// do something
return -1;
} else if (fa->head != fa->tail) {
struct element *tmp_elem = fa->head; // 1
int tmp_val = fa->head->data; // 2
fa->head = fa->head->next; // 3
free(tmp_elem); // 4
return tmp_val; // 5
} else {
struct element *tmp_elem = fa->head; // 1
int tmp_val = fa->head->data; // 2
fa->head = NULL; // 3
fa->tail = NULL; // 3
free(tmp_elem); // 4
return tmp_val; // 5
}
}
void queue_destroy(struct queue *fa) {
while (!queue_is_empty(*fa)) {
queue_dequeue(fa);
}
}
int main() {
struct queue fa; // mémoire est allouée
queue_init(&fa);
printf("is fa empty? %s\n", queue_is_empty(fa) ? "yes" : "no");
printf("enqueuing 10\n");
queue_enqueue(&fa, 10);
printf("enqueuing 20\n");
queue_enqueue(&fa, 20);
printf("enqueuing 30\n");
queue_enqueue(&fa, 30);
printf("is fa empty? %s\n", queue_is_empty(fa) ? "yes" : "no");
int v1 = queue_dequeue(&fa);
printf("v1 contains? %d\n", v1);
printf("head contains? %d\n", queue_head(fa));
printf("tail contains? %d\n", queue_tail(fa));
int v2 = queue_dequeue(&fa);
int v3 = queue_dequeue(&fa);
printf("v2 contains? %d\n", v2);
printf("v3 contains? %d\n", v3);
printf("is fa empty? %s\n", queue_is_empty(fa) ? "yes" : "no");
printf("enqueuing 10\n");
queue_enqueue(&fa, 10);
printf("enqueuing 20\n");
queue_enqueue(&fa, 20);
printf("enqueuing 30\n");
queue_enqueue(&fa, 30);
printf("is fa empty? %s\n", queue_is_empty(fa) ? "yes" : "no");
printf("destroying\n");
queue_destroy(&fa);
printf("is fa empty? %s\n", queue_is_empty(fa) ? "yes" : "no");
return EXIT_SUCCESS;
}
#include <stdlib.h>
#include <stdio.h>
void print_tab(int size, int tab[size]) {
for (int i = 0; i < size; ++i) {
printf("%d ", tab[i]);
}
printf("\n");
}
void swap(int *lhs, int *rhs) {
int tmp = *lhs;
*lhs = *rhs;
*rhs = tmp;
}
int partition(int tab[], int ind_min, int ind_max) {
// ind_min == 0
// ind_max == 6
int pivot = tab[ind_max]; // 12
int i = ind_min - 1; // 0
int j = ind_max; // 6
while (i < j) {
do {
i += 1; // 0, tab[0] == 1 < 12
} while (tab[i] < pivot && i < j);
do {
j -= 1; // j == 5, tab[5] < 12
} while (tab[j] > pivot && i < j);
if (i < j) {
swap(&tab[i], &tab[j]);
}
}
swap(&tab[i], &tab[ind_max]);
return i;
}
void quicksort(int tab[], int ind_min, int ind_max) {
int size = ind_max - ind_min + 1;
if (size > 1) {
int ind_pivot = partition(tab, ind_min, ind_max);
if (ind_pivot - ind_min > 0) {
quicksort(tab, ind_min, ind_pivot - 1);
}
if (ind_max - ind_pivot > 0) {
quicksort(tab, ind_pivot + 1, ind_max);
}
}
}
int main(int argc, char *argv[]) {
int size = argc - 1;
int tab[size];
for (int i = 0; i < size; ++i) {
tab[i] = atoi(argv[i + 1]);
}
print_tab(size, tab);
quicksort(tab, 0, size - 1);
print_tab(size, tab);
}
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10
void tab_initialize(int size, double tab[size]) {
srand(time(NULL));
for (int i = 0; i < size; ++i) {
double rdm = (double)rand() / (double)RAND_MAX;
tab[i] = rdm;
}
}
void tab_show(int size, double tab[size]) {
for (int i = 0; i < size; ++i) {
printf("%lf ", tab[i]);
}
printf("\n");
}
int tab_find_min_index(int i, int size, double tab[size]) {
int i_min = i;
for (int j = i + 1; j < size; ++j) {
if (tab[j] < tab[i_min]) {
i_min = j;
}
}
return i_min;
}
void swap(int i, int j, double tab[]) {
double tmp = tab[i];
tab[i] = tab[j];
tab[j] = tmp;
}
void tab_selection_sort(int size, double tab[size]) {
for (int i = 0; i < size; ++i) {
int i_min = tab_find_min_index(i, size, tab);
// échange tab[i] et tab[j]
if (i_min != i) {
swap(i, i_min, tab);
}
}
}
bool tab_is_sorted(int size, double tab[size]) {
for (int i = 1; i < size; ++i) {
if (tab[i - 1] > tab[i]) {
return false;
}
}
return true;
}
int main() {
// allocate tab
double tab[SIZE];
tab_initialize(SIZE, tab);
tab_show(SIZE, tab);
tab_selection_sort(SIZE, tab);
tab_show(SIZE, tab);
bool is_sorted = tab_is_sorted(SIZE, tab);
printf("Is the tab sorted? %d\n", is_sorted);
}
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
struct element {
int data;
struct element *next;
};
typedef struct element *list;
list list_create() {
return NULL;
}
bool list_is_empty(list l) {
return l == NULL;
}
list list_push(list l, int val) {
struct element *e = malloc(sizeof(*e));
e->next = NULL;
e->data = val;
if (list_is_empty(l)) {
return e;
} else if (val < l->data) {
e->next = l;
return e;
} else {
struct element *crt = l;
while (NULL != crt && val > crt->next->data) {
crt = crt->next;
}
e->next = crt->next;
crt->next = e;
return l;
}
}
list list_extract(list l, int val) {
struct element *crt = l;
struct element *prec = l;
if (list_is_empty(l)) {
return l;
} else if (val == crt->data) {
l = crt->next;
free(crt);
return l;
} else {
while (crt->next != NULL && crt->data < val) {
prec = crt;
crt = crt->next;
}
if (val == crt->data) {
prec->next = crt->next;
free(crt);
}
return l;
}
}
struct element *list_search(list l, int val) {
struct element *crt = l;
while (crt->next != NULL && crt->data < val) {
crt = crt->next;
}
if (val == crt->data) {
return crt;
}
return NULL;
}
void list_destroy(list *l) {
struct element *crt = *l;
while (crt != NULL) {
struct element *e = crt;
crt = crt->next;
free(e);
}
*l = NULL;
}
void print_safe(struct element *e) {
if (e != NULL) {
printf("%d\n", e->data);
}
}
int main() {
list l = list_create();
l = list_extract(l, 2);
l = list_push(l, 10);
l = list_push(l, 0);
l = list_push(l, 2);
printf("first %d\n", l->data);
printf("second %d\n", l->next->data);
printf("third %d\n", l->next->next->data);
struct element *e = list_search(l, 2);
print_safe(e);
/*printf("two %d\n", e->data);*/
e = list_search(l, 10);
print_safe(e);
/*printf("ten %d\n", e->data);*/
e = list_search(l, 0);
print_safe(e);
/*printf("zero %d\n", e->data);*/
e = list_search(l, 3);
print_safe(e);
/*printf("absent %p\n", e);*/
e = list_search(l, 190);
print_safe(e);
/*printf("bigger %p\n", e);*/
e = list_search(l, -1);
print_safe(e);
/*printf("smaller %p\n", e);*/
l = list_extract(l, 2);
l = list_extract(l, 100);
printf("first %d\n", l->data);
printf("second %d\n", l->next->data);
l = list_extract(l, 0);
printf("first %d\n", l->data);
list_destroy(&l);
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#define MAX_CAPACITY 5
struct stack {
int top;
int data[MAX_CAPACITY];
};
void stack_init(struct stack *s) {
s->top = -1;
}
bool stack_is_empty(struct stack s) {
return s.top == -1;
}
void stack_push(struct stack *s, int val) {
assert(s->top < MAX_CAPACITY - 1);
if (s->top != MAX_CAPACITY - 1) {
s->top += 1;
s->data[s->top] = val;
}
}
/*int stack_pop(struct stack *s) {*/
/* if (!stack_is_empty(*s)) {*/
/* int tmp = s->data[s->top];*/
/* s->top -= 1;*/
/* return tmp;*/
/* }*/
/*}*/
/*void stack_pop(struct stack *s, int *val) {*/
/* if (!stack_is_empty(*s)) {*/
/* *val = s->data[s->top];*/
/* s->top -= 1;*/
/* }*/
/*}*/
int *stack_pop(struct stack *s) {
assert(!stack_is_empty(*s));
if (!stack_is_empty(*s)) {
int *val = &(s->data[s->top]);
s->top -= 1;
return val;
} else {
return NULL;
}
}
int *stack_peek(struct stack s) {
if (!stack_is_empty(s)) {
int *val = &(s.data[s.top]);
return val;
} else {
return NULL;
}
}
void stack_print(struct stack s) {
for (int i = s.top; i >= 0; --i) {
printf("%d\n", s.data[i]);
}
}
int main() {
struct stack s;
stack_init(&s);
stack_print(s);
printf("s.top = %d\n", s.top);
printf("is_empty(): %s\n", stack_is_empty(s) ? "True" : "False");
stack_push(&s, 10);
stack_push(&s, 20);
stack_push(&s, 30);
stack_push(&s, 40);
stack_push(&s, 50);
stack_push(&s, 60);
printf("is_empty(): %s\n", stack_is_empty(s) ? "True" : "False");
stack_print(s);
/*int val = -1;*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*printf("popped value = %d\n", stack_pop(&s));*/
/*printf("popped value = %d\n", stack_pop(&s));*/
/*printf("popped value = %d\n", stack_pop(&s));*/
/*printf("popped value = %d\n", stack_pop(&s));*/
/*printf("popped value = %d\n", stack_pop(&s));*/
/*printf("popped value = %d\n", stack_pop(&s));*/
printf("peeked value = %d\n", *stack_peek(s));
printf("peeked value = %d\n", *stack_peek(s));
stack_print(s);
printf("popped value = %d\n", *stack_pop(&s));
printf("popped value = %d\n", *stack_pop(&s));
printf("popped value = %d\n", *stack_pop(&s));
printf("popped value = %d\n", *stack_pop(&s));
printf("popped value = %d\n", *stack_pop(&s));
/*printf("popped value = %d\n", *stack_pop(&s));*/
stack_print(s);
printf("is_empty(): %s\n", stack_is_empty(s) ? "True" : "False");
return EXIT_SUCCESS;
}
size_t strlen(char *str) {
int i = 0;
while (str[i] != '\0') {
i += 1;
}
return i;
}
#include <stdio.h>
int sum(int n) {
if (n <= 1) {
return 1;
} else {
return n + sum(n - 1);
}
}
int sum_imp(int n) {
int i = 1;
int sum = 0;
while (i <= n) {
sum += i;
i += 1;
}
return sum;
}
int main() {
int n = 100;
printf("Sum(%d) = %d, theoretical = %d\n", n, sum(n), n * (n + 1) / 2);
}
#include <stdio.h>
struct key_t {
int k;
};
struct value_t {
int v;
};
struct key_value_t {
struct key_t key;
struct value_t value;
};
struct elem {
struct key_value_t kv;
struct elem *next;
};
struct table {
struct elem *head;
};
struct value_t *sequential(struct table tab, struct key_t key) {
struct elem *e = tab.head;
while (e != NULL) {
if (e->kv.key.k == key.k) {
return &(e->kv.value);
}
e = e->next;
}
return NULL;
}
int main() {
int w = 5;
/*int *v = NULL;*/
int *v = &w;
printf("%d\n", *v);
}
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
bool is_prime(int number) {
for (int i = 2; i <= sqrt(number); i++) {
if (0 == number % i) {
return false;
}
}
return true;
}
int main() {
int max_number = 100000;
for (int i = 2; i <= max_number; ++i) { // max_number * sqrt(max_number)
if (is_prime(i)) {
printf("%d\n", i);
}
}
return 0;
}
slides/figs/kruskal_0.png

53.8 KiB

slides/figs/kruskal_1.png

60 KiB

slides/figs/kruskal_2.png

60.1 KiB

slides/figs/kruskal_3.png

59.8 KiB

slides/figs/kruskal_4.png

59.6 KiB

slides/figs/kruskal_cycle.png

61.1 KiB

slides/figs/kruskal_enonce.png

184 KiB