From 4a3afaf44aa29e66a6c879c60322015a2920a5ab Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Mon, 10 Mar 2025 10:31:33 +0100 Subject: Ajout de la semaine des cours du 3 au 7 mars --- .../td/5- cha\303\256nes de caract\303\250re.md" | 9 +------- .../td/6- r\303\251cursivit\303\251.md" | 25 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 "semestre 2/informatique/td/6- r\303\251cursivit\303\251.md" (limited to 'semestre 2/informatique/td') diff --git "a/semestre 2/informatique/td/5- cha\303\256nes de caract\303\250re.md" "b/semestre 2/informatique/td/5- cha\303\256nes de caract\303\250re.md" index 0587412..8bea089 100644 --- "a/semestre 2/informatique/td/5- cha\303\256nes de caract\303\250re.md" +++ "b/semestre 2/informatique/td/5- cha\303\256nes de caract\303\250re.md" @@ -28,14 +28,7 @@ int int_to_str(int val){ for (i = 1; tmp_val % 10 != tmp_val; i++) tmp_val /= 10; char *str = malloc(sizeof(char) * (i+1)); // +1 car on n'oublie pas le '\0' str[i] = '\0'; // on rajoute de suite le '\0' - for (int j = 0; j <= i; j++) { - int c = val % pow(10, j); - val -= c; - str[j] = c + 48; - } + for (int j = 0; j < i; j++) str[j-1] = val % pow(10, j) + 48; return str; } ``` - -On ne peut pas écrire `int arr[][];` ! -|> on doit forcément indiquer la taille des sous-tableaux, e.g. `int arr[][5];` diff --git "a/semestre 2/informatique/td/6- r\303\251cursivit\303\251.md" "b/semestre 2/informatique/td/6- r\303\251cursivit\303\251.md" new file mode 100644 index 0000000..94124e3 --- /dev/null +++ "b/semestre 2/informatique/td/6- r\303\251cursivit\303\251.md" @@ -0,0 +1,25 @@ +--- +tags: + - sorbonne + - informatique + - td +semestre: 2 +--- +Fonction récursive pour trouver si deux chaînes sont égales +```c +int str_equal(char *s1, char *s2){ + int b = *s1 != *s2; + if (b || *s1 == '\0') return !b; + return str_equal(s1+1, s2+1); +} +``` + +Recherche par dichotomie : +```c +int dichotomie(int *a, int e, int taille){ + if (taille <= 0) return -1; + int m = (start+end) / 2; + if (a[m] == e) return m; + return dichotomie(e > a[m] ? tab+m+1 : tab, e, taille-m-1); +} +``` -- cgit v1.2.3