diff --git a/APL1.1/TP12/balayage.c b/APL1.1/TP12/balayage.c index 7e5eee4..45eacc6 100644 --- a/APL1.1/TP12/balayage.c +++ b/APL1.1/TP12/balayage.c @@ -3,16 +3,38 @@ //A ignorer, incomplet. +int sum(int x) { + int somme = 0; + for (int i = 0; i <= x; i++) { + somme += i; + } + + return somme; +} + int main(int argc, char * argv[]) { - int table[450] = {} - table[0] = 1; - table[1] = 1; - table[2] = 1; + int table[900] = {}; + + int inc = 1; + int index = 0; + for (int i = 0; i < 30; i++) { + index += inc; + table[index] = 1; + table[index+1] = 1; + inc++; + } + for (int i = 0; i < 30; i++) { + int startIndex, endIndex = sum(i+1); + startIndex = endIndex - i; - - for (int i = 3; i < 900; i++) { - + for (int j = startIndex; j <= endIndex; j++) { + if (j != startIndex && j != endIndex) { + table[j] = table[j-i] + table[j-i-1]; + } + printf("%3d ", table[j]); + } + printf("\n"); } return EXIT_SUCCESS; } diff --git a/APL1.1/TP12/magique.c b/APL1.1/TP12/magique.c index c310b0a..55cac75 100644 --- a/APL1.1/TP12/magique.c +++ b/APL1.1/TP12/magique.c @@ -19,14 +19,40 @@ int main(int argc, char * argv[]) { int numcheck[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; int carre[3][3] = {}; - for (int i = 0; i < 9; i ++) { - printf("Valeur case n°%d : ", i+1); - scanf("%d", carre[i%3][i/3]); + int nb_val = 0; + do { + int val; + printf("Veuillez entrer la valeur n°%d : ", nb_val+1); + int result = scanf("%d", &val); + + if (result == 1) { + carre[nb_val/3][nb_val%3] = val; + nb_val++; + } + } while (nb_val < 9); + + int anchor = carre[0][0] + carre[0][1] + carre[0][2]; + for (int i = 0; i < 7; i++) { + int condition_respected = 0; + + if (i < 3) { + condition_respected = carre[i][0] + carre[i][1] + carre[i][2] == anchor; + } else if (i < 6) { + condition_respected = carre[0][i-3] + carre[1][i-3] + carre[2][i-3] == anchor; + } else if (i == 6) { + condition_respected = carre[0][0] + carre[1][1] + carre[2][2] == anchor; + } else { + condition_respected = carre[2][0] + carre[1][1] + carre[0][2] == anchor; + } + + + if (!condition_respected) { + printf("Ceci n'est pas un carré magique.\n"); + return EXIT_SUCCESS; + } } - for (int i = 0; i < 9; i++) { - if () - } + printf("Votre carré est magique !\n"); return EXIT_SUCCESS; }