167 lines
3.8 KiB
C
167 lines
3.8 KiB
C
#include <assert.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
|
|
int spitze(const bool t[], int size) {
|
|
for (int i = 0; i < size; i += 1) {
|
|
if (t[0] ^ t[i]) {
|
|
return i + 1;
|
|
}
|
|
}
|
|
return size;
|
|
}
|
|
|
|
bool nul(const bool t[], int size) {
|
|
while (size > 0 && !t[size - 1]) {
|
|
size -= 1;
|
|
}
|
|
int count = 0;
|
|
for (int i = 0; i < size; i += 1) {
|
|
count += t[i];
|
|
if (count <= i / 2) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
double modfloat(double a, double b) {
|
|
int q = (int)(a / b);
|
|
return a - (double)(b * q);
|
|
}
|
|
|
|
void minmax(const int t[], int size, int *min, int *max) {
|
|
if (size) {
|
|
*min = t[0];
|
|
*max = t[0];
|
|
}
|
|
for (int i = 1; i < size; i += 1) {
|
|
if (t[i] < *min) {
|
|
*min = t[i];
|
|
} else if (t[i] > *max) {
|
|
*max = t[i];
|
|
}
|
|
}
|
|
}
|
|
|
|
int medianemax(const int t[], int size) {
|
|
if (!size) {
|
|
return 0;
|
|
}
|
|
bool forward = true;
|
|
int min = 0;
|
|
int max = size - 1;
|
|
while (min < max) {
|
|
if (forward) {
|
|
min += 1;
|
|
if (t[min] >= t[max]) {
|
|
forward = false;
|
|
}
|
|
} else {
|
|
max -= 1;
|
|
if (t[max] >= t[min]) {
|
|
forward = true;
|
|
}
|
|
}
|
|
}
|
|
return min;
|
|
}
|
|
|
|
void derive(double t[], int size) {
|
|
for (int i = 1; i < size; i += 1) {
|
|
t[i - 1] = i * t[i];
|
|
}
|
|
t[size - 1] = 0;
|
|
}
|
|
|
|
double *multpol(const double t1[], const double t2[], int size1, int size2) {
|
|
if (size1 == 0 || size2 == 0) {
|
|
return NULL;
|
|
}
|
|
int size3 = size1 - 1 + size2;
|
|
double *t3 = malloc(size3 * sizeof(double));
|
|
// On s'économise l'initialisation
|
|
for (int i = 0; i < size1; i += 1) {
|
|
t3[i] = t1[i] * t2[0];
|
|
}
|
|
for (int j = 1; j < size2; j += 1) {
|
|
t3[j + size1 - 1] = t1[size1 - 1] * t2[j];
|
|
}
|
|
|
|
for (int i = 0; i < size1 - 1; i += 1) {
|
|
for (int j = 1; j < size2; j += 1) {
|
|
t3[i + j] += t1[i] * t2[j];
|
|
}
|
|
}
|
|
|
|
return t3;
|
|
}
|
|
|
|
// int main() {
|
|
// double t1[] = {2.1, 3.1, 5.1};
|
|
// double t2[] = {2, 3};
|
|
// double t3[] = {4.2, 12.5, 19.5, 15.299999999999999};
|
|
// double *res = multpol(t1, t2, 3, 2);
|
|
// for (int i = 0; i < 4; i += 1) {
|
|
// assert(t3[i] == res[i]);
|
|
// }
|
|
// free(res);
|
|
// }
|
|
|
|
char *cesar(const char str[], int a, int b) {
|
|
char map[26];
|
|
// C'est moche mais ça ne change pas la complexité. Sur de longues
|
|
// chaines ça doit même être plus efficace de calculer une map à l'avance.
|
|
for (char c = 0; c < 26; c += 1) {
|
|
int r = ((c * a) + b) % 26;
|
|
if (r < 0) {
|
|
r += 26;
|
|
}
|
|
for (char i = 0; i < c; i += 1) {
|
|
if (map[i] == r) {
|
|
assert(false && "la fonction n'est pas bijective");
|
|
}
|
|
}
|
|
map[c] = r;
|
|
}
|
|
|
|
// On peut faire plus efficace en consommant str et en stockant res dedans
|
|
char *res = malloc(strlen(str) * sizeof(char));
|
|
|
|
for (int i = 0; str[i] != 0; i += 1) {
|
|
if (str[i] >= 'a' && str[i] <= 'z') {
|
|
res[i] = 'a' + map[(str[i] - 'a')];
|
|
} else if (str[i] >= 'A' && str[i] <= 'Z') {
|
|
res[i] = 'A' + map[(str[i] - 'A')];
|
|
} else {
|
|
res[i] = str[i];
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
// int main() {
|
|
// char chaine1[] = "Hello world!";
|
|
|
|
// char *res1 = cesar(chaine1, -3, -5);
|
|
// printf("resultat 1 : %s\n", res1);
|
|
|
|
// free(res1);
|
|
|
|
// int c = 13;
|
|
// int d = 5;
|
|
// char *res2 = cesar(chaine1, c, d);
|
|
// printf("resultat 2 : %s \n", res2);
|
|
// free(res2);
|
|
|
|
// exit(0);
|
|
// }
|
|
|
|
char *auguste(int n) {
|
|
assert(n >= 1 && n <= 3999);
|
|
char *str =
|
|
} |