Advertisement
Guest User

Carré Magique

a guest
Jan 25th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.    bool magique = true;
  6.    int tailleM;
  7.    cin >> tailleM;
  8.    int freqNbres[(tailleM^2) + 1] = {0};
  9.    int carreM [tailleM][tailleM];
  10.    for (int lig = 0; lig < tailleM; lig ++)
  11.    {
  12.       for (int col = 0; col < tailleM; col ++)
  13.       {
  14.          int valeur;
  15.          cin >> valeur;
  16.          carreM [lig][col] = valeur;
  17.          if (carreM [lig][col] <= (tailleM^2))
  18.             freqNbres[carreM [lig][col]] ++;
  19.       }
  20.    }
  21.    for (int nombre = 1; nombre <= (tailleM^2); nombre ++)
  22.    {
  23.       if (freqNbres [nombre] != 1)
  24.          magique = false;
  25.    }
  26.    int valeurRef = 0;
  27.    for (int nombre = 0; nombre < tailleM; nombre ++)
  28.       valeurRef += carreM[0][nombre];
  29.    
  30.    for (int lig = 0; lig < tailleM; lig ++)
  31.    {
  32.       int sommeC = 0;
  33.       for (int col = 0; col < tailleM; col ++)
  34.          sommeC += carreM [lig][col];
  35.       if (sommeC != valeurRef)
  36.          magique = false;
  37.    }
  38.     for (int col = 0; col < tailleM; col ++)
  39.    {
  40.       int sommeC = 0;
  41.       for (int lig = 0; lig < tailleM; lig ++)
  42.          sommeC += carreM [lig][col];
  43.       if (sommeC != valeurRef)
  44.          magique = false;
  45.    }
  46.    
  47.    int sommeDiag = 0;
  48.    for (int nombre = 0; nombre < tailleM; nombre ++)
  49.       sommeDiag += carreM[nombre][nombre];
  50.    if (sommeDiag != valeurRef)
  51.       magique = false;
  52.    
  53.    sommeDiag = 0;
  54.    for (int nombre = 0; nombre < tailleM; nombre ++)
  55.       sommeDiag += carreM[tailleM - nombre - 1][nombre];
  56.    if (sommeDiag != valeurRef)
  57.       magique = false;
  58.    if (magique)
  59.       cout << "yes";
  60.    else
  61.       cout << "no";
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement