█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 1155

Problema 0xe1 - Cuadrado mágico

  • Autor: EduardoVega
  • Fecha: 2020-10-11 03:25:16 UTC (Hace alrededor de 4 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.004 s 1 KBi
#2
Correcto
0.004 s 1 KBi
#3
Correcto
0.004 s 1 KBi
#4
Incorrecto
0.004 s 1 KBi
#5
Correcto
0.004 s 1 KBi
#6
Correcto
0.004 s 1 KBi
#7
Incorrecto
0.003 s 1 KBi
#8
Correcto
0.004 s 1 KBi
#9
Correcto
0.004 s 1 KBi
#10
Correcto
0.004 s 1 KBi
#11
Correcto
0.004 s 1 KBi
#12
Correcto
0.004 s 1 KBi
#13
Correcto
0.004 s 1 KBi
#14
Correcto
0.004 s 1 KBi
#15
Correcto
0.004 s 1 KBi
#16
Correcto
0.006 s 1 KBi
#17
Correcto
0.004 s 1 KBi
#18
Correcto
0.005 s 1 KBi
#19
Correcto
0.006 s 1 KBi
#20
Correcto
0.006 s 1 KBi
Puntos totales: 90 / 100

Código

/* Code by Eduardo Vega */
#include <stdio.h>
#include <stdlib.h>

/* Free function for the allocated matrix */
void my_free(int **matrix, int size)
{
  int i, j;

  for (i = 0; i < size; i++)
      free(matrix[i]);
  
  free(matrix);   
} 

int main()
{ 
  int suma = 0, tmp = 0, size = 0, num = 0, 
  i, j;
  int **matrix = NULL;

  scanf("%d", &size);

  /* Matrix is allocated using double pointer */
  matrix = malloc(sizeof(int *) * size);
  for (i = 0; i < size; i++)  
      matrix[i] = malloc(sizeof(int) * size);

  /* Fill the matrix with the input numbers */
  for (i = 0; i < size; i++)
  {
    for (j = 0; j < size; j++)
    {
      scanf("%d", &num);
      matrix[i][j] = num; 
      
      /* Get the sum of the first row */ 
      if (i == 0)
          suma += num;
    }
  } 
  
  /* Check matrix horizontally and compare */
  for (i = 0; i < size; i++)
  {
    for (j = 0; j < size; j++)    
        tmp += matrix[i][j];

    if (tmp != suma)
    { 
      printf("No\n");
      my_free(matrix, size);
      return 0;
    }
    tmp = 0;   
  }
  
  /* Check matrix vertically and compare */
  for (j = 0; j < size; j++)
  {
    for (i = 0; i < size; i++)    
        tmp += matrix[i][j];

    if (tmp != suma)
    { 
      printf("No\n");
      my_free(matrix, size);
      return 0;
    } 
    tmp = 0;   
  }
  
  /* Check first diagonal and compare */  
  for (i = 0; i < size; i++)      
      tmp += matrix[i][i];    
   
  if (tmp != suma)
  { 
    printf("No\n");
    my_free(matrix, size);
    return 0;
  } 
  tmp = 0;

  /* Check second diagonal and compare */   
  for (i = size - 1; i >= 0; i--)  
      tmp += matrix[i][i];    
  
  if (tmp != suma)
  { 
    printf("No\n");
    my_free(matrix, size);
    return 0;
  } 

  printf("Yes\n");
  my_free(matrix, size);
  return 0;
}