diff options
Diffstat (limited to 'semestre 2/informatique/tme/semaine5')
3 files changed, 74 insertions, 15 deletions
diff --git a/semestre 2/informatique/tme/semaine5/28_participation_frais.c b/semestre 2/informatique/tme/semaine5/28_participation_frais.c index e5aff0d..96da9b7 100644 --- a/semestre 2/informatique/tme/semaine5/28_participation_frais.c +++ b/semestre 2/informatique/tme/semaine5/28_participation_frais.c @@ -13,9 +13,10 @@ void init_tab(float tab[NB_AMIS][NB_JOURS]){ void random_tab(float tab[NB_AMIS][NB_JOURS], int j) { int a = rand()%NB_AMIS; int v = rand()%21+30; - tab[a][j] = v; + float to_pay = (float) v/(-NB_AMIS); + tab[a][j] = v + to_pay; for (int i = 0; i < NB_AMIS; i++) { - if (i != a) tab[i][j] = (float) v/(-NB_AMIS+1); + if (i != a) tab[i][j] = to_pay; } } diff --git a/semestre 2/informatique/tme/semaine5/30_compression.c b/semestre 2/informatique/tme/semaine5/30_compression.c index 161cd00..154dbc9 100644 --- a/semestre 2/informatique/tme/semaine5/30_compression.c +++ b/semestre 2/informatique/tme/semaine5/30_compression.c @@ -14,15 +14,19 @@ int init_tab(int tab[MAX+1]) { } void compress_tab(int tab_brut[], int tab_compress[]){ - int j = 0; int last = -1; int cnt = 0; + int w = 0; for (int i = 0; tab_brut[i] != -1; i++) { if (last == -1 || tab_brut[i] != last) { if (last != -1) { - int id = j++; - tab_compress[id*2] = cnt; - tab_compress[id*2+1] = last; + if (cnt == 1) { + tab_compress[w] = last; + } else { + tab_compress[w] = cnt; + tab_compress[++w] = last; + } + w++; } last = tab_brut[i]; cnt = 0; @@ -30,11 +34,35 @@ void compress_tab(int tab_brut[], int tab_compress[]){ cnt++; } if (last != -1) { - int id = j++; - tab_compress[id*2] = cnt; - tab_compress[id*2+1] = last; + if (cnt == 1) { + tab_compress[w] = last; + } else { + tab_compress[w] = cnt; + tab_compress[++w] = last; + } + w++; + } + tab_compress[w] = -1; +} + +void decompress(int tab_brut[], int tab_compress[]) { + int i = 0; + int w = 0; + while (tab_compress[i] != -1) { + int v = tab_compress[i]; + if (v < 2) { + tab_brut[w++] = v; + } else { + int ins_v = tab_compress[++i]; + for (int j = 0; j < v; j++) { + tab_brut[w++] = ins_v; + } + } + for (int j = 0; j < w; j++) printf("%d, ", tab_brut[j]); + printf("\n"); + i++; } - tab_compress[j*2] = -1; + tab_brut[w] = -1; } int compare(int tab_brut[], int tab_compress[]){ @@ -51,7 +79,31 @@ int compare(int tab_brut[], int tab_compress[]){ int main(){ srand(time(NULL)); - int tab[MAX+1] = {}; + int tab1[]={0,0,1,1,1,1,0,1,1,1,0,0,1,0,0,0,0,-1}; + //int tab1[] = {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,-1}; + for (int i = 0; tab1[i] != -1; i++) printf("%d, ", tab1[i]); + printf("\n"); + int tab2[20]; + compress_tab(tab1,tab2); + int tab3[20]; + decompress(tab3, tab2); + int i; + for (i = 0; tab1[i] != -1 && tab3[i] != -1; i++) { + printf("%d - %d\n", tab1[i], tab3[i]); + } + printf("%d\n", i); + i = 0; + while (tab2[i] != -1) { + int v = tab2[i]; + if (v < 2) { + printf("%d ", v); + } else { + printf("%d:%d ", v, tab2[++i]); + } + i++; + } + printf("\n"); + /*int tab[MAX+1] = {}; int tab_compress[MAX+1] = {}; init_tab(tab); compress_tab(tab, tab_compress); @@ -59,6 +111,6 @@ int main(){ printf("\n"); for (int i = 0; tab_compress[i] != -1; i += 2) printf("%d:%d, ", tab_compress[i], tab_compress[i+1]); printf("\n"); - printf("%s", compare(tab, tab_compress) ? "True" : "False"); + printf("%s", compare(tab, tab_compress) ? "True" : "False");*/ return 0; } diff --git a/semestre 2/informatique/tme/semaine5/31_image_mystere.c b/semestre 2/informatique/tme/semaine5/31_image_mystere.c index 51ca321..29264e1 100644 --- a/semestre 2/informatique/tme/semaine5/31_image_mystere.c +++ b/semestre 2/informatique/tme/semaine5/31_image_mystere.c @@ -12,9 +12,15 @@ void calcule_borne_sup(int *tab, int taille){ int tire_non_equi(int *tab, int taille){ int tire = rand()%100; - int i; - for (i = taille -1; tab[i] > tire; i--); - printf("%d - %d <= %d\n", i, tab[i], tire); + int i = taille -1; + /*while (i >= 0) { + if (tire > tab[i]) break; + i--; + } + i++;*/ + for (i = taille -1; tab[i] >= tire; i--); + i++; + printf("%d >= %d\n", tab[i], tire); return i; } |
