#include <stdlib.h>
#include <stdio.h>
int eh_magico( int ** quad, int n )
{
int i = 0, j = 0;
int x = 0, y = 0;
int d1 = 0, d2 = 0;
/* Somatorio das diagonais */
for( i = 0; i < n; i++ )
{
d1 += quad[i][i];
d2 += quad[n - i - 1][i];
}
/* Verifica se diagonais sao iguais */
if( d1 != d2 )
return -1;
for( i = 0; i < n; i++ )
{
x = 0;
y = 0;
/* Somatorio linha e coluna */
for( j = 0; j < n; j++ )
{
x += quad[i][j];
y += quad[j][i];
}
/* Verifica linha e coluna */
if( x != d1 || y != d1 )
return -1;
}
/* Eh magico! */
return d1;
}
int main( void )
{
int lin, col, dim;
int ** quadrado_misterioso;
/* Le dimensoes */
/* Aloca memoria para n linhas */
quadrado_misterioso
= malloc( dim
* sizeof(int*) );
/* Para cada linha */
for( lin = 0; lin < dim; lin++ )
{
/* Aloca memoria para n colunas da linha atual */
quadrado_misterioso
[lin
] = malloc( dim
* sizeof(int) );
/* Para cada coluna da linha */
for( col = 0; col < dim; col++ )
scanf( "%d", &quadrado_misterioso
[lin
][col
] ); }
/* Verifica se eh um quadrado magico */
printf("%d\n", eh_magico
( quadrado_misterioso
, dim
) );
/* Libera memoria ocupada pelo quadrado */
for( lin = 0; lin < dim; lin++ )
free(quadrado_misterioso
[lin
]); free(quadrado_misterioso
);
return 0;
}
I2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8c3RkaW8uaD4KCmludCBlaF9tYWdpY28oIGludCAqKiBxdWFkLCBpbnQgbiApCnsKCWludCBpID0gMCwgaiA9IDA7CglpbnQgeCA9IDAsIHkgPSAwOwoJaW50IGQxID0gMCwgZDIgPSAwOwoKCS8qIFNvbWF0b3JpbyBkYXMgZGlhZ29uYWlzICovCglmb3IoIGkgPSAwOyBpIDwgbjsgaSsrICkKCXsKCQlkMSArPSBxdWFkW2ldW2ldOwoJCWQyICs9IHF1YWRbbiAtIGkgLSAxXVtpXTsKCX0KCgkvKiBWZXJpZmljYSBzZSBkaWFnb25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