couples_entiers.c (2835B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <math.h> 4 5 int pair(int x){ 6 return (!(x % 2)); 7 } 8 9 int orderedPairToCode(int x, int y){ 10 int sumxy, code; 11 sumxy = x + y; 12 code = ((sumxy)*(sumxy + 1))/2; 13 14 if(pair(sumxy)){ 15 code = code + x; 16 } 17 else{ 18 code = code + y; 19 } 20 return code; 21 } 22 23 24 int* codeToOrderedPair(int code){ 25 int *couple = malloc(2*sizeof(int)); 26 int n = sqrt(code * 2); 27 int axis = (n * (n + 1))/2; 28 int diff = 0; 29 if(axis > code){ 30 while(axis > code){ 31 n = n - 1; 32 axis = (n * (n + 1))/2; 33 } 34 } 35 else if(axis < code){ 36 while(axis < code){ 37 n = n + 1; 38 axis = (n * (n + 1))/2; 39 } 40 if(axis > code){ 41 n = n - 1; 42 axis = (n * (n + 1))/2; 43 } 44 } 45 46 if(axis == code){ // je pense que je peux me dispenser de cet "if", ça revient au même car diff serait = 0 47 if(pair(n)){ 48 couple[0] = 0; 49 couple[1] = n; 50 } 51 else{ 52 couple[0] = n; 53 couple[1] = 0; 54 } 55 } 56 if(axis != code){ // idem 57 diff = code - axis; 58 if(pair(n)){ 59 couple[0] = diff; 60 couple[1] = n - diff; 61 } 62 else{ 63 couple[0] = n - diff; 64 couple[1] = diff; 65 } 66 } 67 return couple; 68 } 69 70 int orderedMultipleToCode(int *arr, int size){ 71 int code; 72 if(size > 1){ 73 code = orderedPairToCode(arr[size - 2], arr[size - 1]); 74 arr[size - 2] = code; 75 arr = realloc(arr, (size - 1)); 76 if(size > 2){ 77 code = orderedMultipleToCode(&arr[0], (size - 1)); 78 } 79 } 80 return code; 81 } 82 83 int* codeToOrderedMultiple(int code, int size){ 84 int *multiple = malloc(size*sizeof(int)); 85 int *pair; 86 int i = 0; 87 for(i = 0; i < (size - 1); i++){ 88 pair = codeToOrderedPair(code); 89 code = pair[1]; 90 multiple[i] = pair[0]; 91 multiple[i + 1] = pair[1]; 92 } 93 return multiple; 94 } 95 96 97 int main(int argc, char **argv, char **envp){ 98 99 int x = 0; 100 int y = 0; 101 int code = 0; 102 int *p; 103 int size = 0; 104 105 do{ 106 /* 107 printf ("\nx = "); 108 scanf("%d",&x); 109 printf ("y = "); 110 scanf("%d",&y); 111 code = orderedPairToCode(x, y); 112 printf("Le code du couple (%d, %d) est %d\n", x, y, code); 113 114 printf ("\ncode = "); 115 scanf("%d",&code); 116 p = codeToOrderedPair(code); 117 printf("The ordered pair identified by code %d is (%d, %d)", code, p[0], p[1]); 118 119 */ 120 printf("\nEnter a size of a multidimensional array representing a 'ordered multiple': "); 121 scanf("%d",&size); 122 p = malloc(size * sizeof(int)); 123 int i; 124 for(i = 0; i < size; i++){ 125 printf("Enter value number %d: ", i); 126 scanf("%d", &p[i]); 127 } 128 129 code = orderedMultipleToCode(p, size); 130 printf("\n... The code is %d", code); 131 132 133 134 printf ("\ncode = "); 135 scanf("%d",&code); 136 printf ("\nnumber of ordered elements = "); 137 scanf("%d",&size); 138 p = codeToOrderedMultiple(code, size); 139 printf("The ordered multiple identified by code %d contains the following elements: ", code); 140 printf("("); 141 for(i = 0; i < (size - 1); i++){ 142 printf("%d, ", p[i]); 143 } 144 printf("%d)", p[size-1]); 145 146 147 } 148 while(code != -1); 149 } 150