This commit is contained in:
2026-02-09 03:43:16 +01:00
parent 5082f62e59
commit d6eecf0cee
8 changed files with 143 additions and 20 deletions
+49
View File
@@ -0,0 +1,49 @@
#include <stdlib.h>
//#include <stdio.h>
int *compress_array(const int *input, int n, int *out_size){
if (input == NULL) return NULL;
int taille = 1;
for (int i = 0; i < n -1; i++) if(input[i] != input[i + 1]) taille ++;
*out_size = taille * 2;
int *rslt = malloc(*out_size * sizeof(int));
if (rslt == NULL) return NULL;
int index_rslt = 0;
int index = 0;
while(index < n){
int ch_cons = 0;
int chiffre = input[index];
while (index < n && input[index] == chiffre){
ch_cons ++;
index ++;
}
rslt[index_rslt ++] = chiffre;
rslt[index_rslt ++] = ch_cons;
}
return rslt;
}
/*
int main(){
int test[] = {3, 3, 3, 1, 1, 5, 5, 5, 5};
int sortie = 6;
int *rs = compress_array(test, 9, &sortie);
for(int i = 0; i < sortie; i++){
printf("%i;", rs[i]);
}
free(rs);
}*/