j'ai 2 tableau tab1[6]={0,2,5,1,4,3} et tab2[6]={0,5,3,2,1,4} , je dois prendre la partie {5,1,4} du tab1 extraire l'ordre de ces element {3eme, 1er,2eme} {3,1,2} , faire la méme chose pour les elemnts {3,2,1} du tab2 est avoir l'ordre correspondant qui est :{3,2,1} , puis echanger la partie l'order du {5,1,4} par selon l'ordre qu'on a extrait du tab2 : {3,2,1} : echanger {5,1,4} d'ordre {3,1,2} par l'ordre {3,2,1} ===> {5,4,1} and get tab1[6] ={0,2,5,4,1,3} , et la méme chose pour tab2. actuelement j'ai pu faire la fonction qui extrait l'ordre des 3 element et celle qui fait l'échange des elements , mais ça marche que pour les 3 elements du tableau pas le tableau tous entier :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 #include <stdio.h> #define swap(a, b) {(a)^=(b); (b)^=(a); (a)^=(b);} void getOrder(int tab[], int n, int pos[]) { for (int i = 0; i < n; i++) { int count = 0; for (int j = 0; j < n; j++) count += (tab[j] < tab[i]); pos[i] = count; } } void exchange(int *A,int *I,int n){ int i, j, k; for(i = 0; i < n ; i++){ if(i != I[i]){ j = i; while(i != (k = I[j])){ swap(A[j], A[k]); I[j] = j; j = k; } I[j] = j; } } } int main() { int tab[3] = { 5, 1, 4 }; int tab2[3] = { 3, 2, 1 }; int pos1[3]; int pos2[3]; int n = sizeof(tab) / sizeof(tab[0]); getOrder(tab, n, pos1); for (int i = 0; i < n; i++) printf("%d ", pos1[i]); printf("\n"); getOrder(tab2, n, pos2); for (int i = 0; i < n; i++) printf("%d ", pos2[i]); printf("\n"); exchange(tab,pos2,n); for (int i = 0; i < n; i++){ printf("%d ", tab[i]); } return 0; }
Partager