1
The integers positioned on an Nxn square such that all lines and main diagonal have the same sum.
For example, the square below
2 7 11 9 5 6 4 3 13
is a mysterious square of sum 20, since all lines (2+7+11 = 20, 9+5+6 = 20 and 4+3+13 = 20) and main diagonal (2 + 5 + 13 = 20) have the same sum (20).
Write a program that, given a square, determines if it is mysterious or not and what its sum is (if it is magical).
Entree
The first line contains an integer N. The following lines contain N integers each, separated by exactly one blank.
Exit
Your program should print, in the standard output, a single line with an integer representing the sum of the magic square or -1 if the square is not magic.
PS: I can’t use vector, I can only use repeating structures.
EXAMPLE 1
ENTREE:
3 2 7 11 9 5 6 4 3 13
EXIT:
20
PS: My exit was:
-1
Just follow my code:
int main()
{
int n, i, j, elem;
int , soma_linhas = 0, soma_dp = 0;
scanf("%d", &n);
//leitura dos elementos
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
scanf("%d", &elem);
//somatorio de cada linha
aux += i;
soma_linhas = aux;
}
}
//somatorio da diagonal principal
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
if(i == j){
aux += i + j;
soma_dp = aux;
}
}
}
if(soma_linhas == soma_dp)
printf("%d", soma_linhas);//se for quadrado misterioso tanto faz mostra a linhas ou da diagonal
else
printf("-1");
return 0;
}
Where am I going wrong?
Your input example is not a magic square. The sum of none of the columns is
20
. The expected output would be-1
.– Lacobus
That is the question. It is a mysterious square not a magician. That is why it should be only the sum of the lines and the main diagonal.
– Denilson Silva