#include #include #include int main(void) { int sarr[9]; int k=0; int a[3][3]; int total = 0; int cost = INT_MAX; int temp = 0; for(int a_i = 0; a_i < 3; a_i++){ for(int a_j = 0; a_j < 3; a_j++){ scanf("%d",&a[a_i][a_j]); total += a[a_i][a_j]; } } //printf("Total of matrix: %d\n",total); //flatten array for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { sarr[k] = a[i][j]; k++; } } //possible 3x3 magic squares only eight int matrix[8][9]={{4,9,2,3,5,7,8,1,6},{2,7,6,9,5,1,4,3,8},{6,1,8,7,5,3,2,9,4},{8,3,4,1,5,9,6,7,2},{2,9,4,7,5,3,6,1,8},{6,7,2,1,5,9,8,3,4},{8,1,6,3,5,7,4,9,2},{4,3,8,9,5,1,2,7,6}}; for (int i=0;i<8;i++) { temp = 0; for(int j=0;j<9;j++) { temp+=abs(sarr[j]-matrix[i][j]); } //printf("Temp of magic square matrix: %d\n",temp); if(temp < cost) cost = temp; //printf("Cost: %d\n",cost); } printf("%d ",cost); return 0; }