eg
This commit is contained in:
@@ -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);
|
||||
|
||||
}*/
|
||||
|
||||
Reference in New Issue
Block a user