Advertisement
rdsedmundo

Quadrado mágico.c

Jul 4th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4.     int nLado, Soma = 0, sN = 0, __has = 0;
  5.  
  6.     scanf("%d", &nLado);
  7.  
  8.     int Quad[nLado][nLado], i, j;
  9.     int Has[1024 * 1024];
  10.  
  11.     for(i = 0; i < 1024 * 1024; i++)
  12.     Has[i] = 0;
  13.  
  14.     for(i = 0; i < nLado; i++) {
  15.         for(j = 0; j < nLado; j++) {
  16.         int num;
  17.             scanf("%d", &num);
  18.  
  19.         if(Has[num - 1] == 0) {
  20.         Quad[i][j] = num;
  21.         Has[num - 1] = 1;
  22.         } else {
  23.         __has = 1;
  24.         }
  25.         }
  26.     }
  27.  
  28.     for(j = 0; j < nLado; j++)
  29.     if(!__has)
  30.         Soma += Quad[0][j];
  31.  
  32.     for(i = 0; i < nLado && Soma != (0); i++) {
  33.         sN = 0;
  34.         for(j = 0; j < nLado; j++) {
  35.             sN += Quad[i][j];
  36.         }
  37.  
  38.         if(sN != Soma)
  39.             Soma = 0;
  40.     }
  41.  
  42.     for(i = 0; i < nLado && Soma != (0); i++) {
  43.         sN = 0;
  44.         for(j = 0; j < nLado; j++)
  45.             sN += Quad[j][i];
  46.  
  47.         if(sN != Soma)
  48.             Soma = 0;
  49.     }
  50.  
  51.     sN = 0;
  52.  
  53.     for(j = 0; j < nLado && Soma != (0); j++)
  54.       sN += Quad[j][j];
  55.  
  56.     if(sN != Soma)
  57.         Soma = 0;
  58.  
  59.     sN = 0;
  60.  
  61.     for(j = nLado-1; j >= 0 && Soma != (0); j--)
  62.         sN += Quad[nLado - 1 - j][j];
  63.  
  64.     if(sN != Soma)
  65.         Soma = 0;
  66.  
  67.     printf("%d\n", Soma);
  68.  
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement