Advertisement
a53

PatratMagic3

a53
Jan 27th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <iostream>
  2. #define N 100
  3. using namespace std;
  4. int PM[N][N];
  5.  
  6. void GenPmi(int n)
  7. {
  8. int i=n/2;
  9. int j=n-1;
  10. for(int num=1;num<=n*n;)
  11. {
  12. if(i==-1&&j==n)
  13. j=n-2,i=0;
  14. else
  15. {
  16. if(j==n)
  17. j=0;
  18. if(i<0)
  19. i=n-1;
  20. }
  21. if(PM[i*2][j*2])
  22. {
  23. j-=2;
  24. ++i;
  25. continue;
  26. }
  27. else
  28. PM[i*2][j*2]=num++;
  29. ++j;--i;
  30. }
  31. }
  32.  
  33. int main()
  34. {
  35. int n;
  36. cin>>n;
  37. /// Pas 1 (L=-1,U=-2,X=-3)
  38. for(int i=1;i<=n/2;i+=2)
  39. for(int j=1;j<n;j+=2)
  40. PM[i][j]=-1;
  41. for(int j=1;j<n;j+=2)
  42. PM[n/2+2][j]=-2;
  43. for(int i=n/2+4;i<n;i+=2)
  44. for(int j=1;j<n;j+=2)
  45. PM[i][j]=-3;
  46. /// Pas 2
  47. swap(PM[n/2][n/2],PM[n/2+2][n/2]);
  48. /// Pas 3
  49. GenPmi(n/2); /// Generez patratul magic impar 2k+1
  50. /// Pas 4
  51. for(int i=1;i<n;i+=2)
  52. for(int j=1;j<n;j+=2)
  53. {
  54. if(PM[i][j]==-1)
  55. PM[i-1][j-1]*=4,PM[i][j]=PM[i-1][j-1]-1,PM[i][j-1]=PM[i-1][j-1]-2,PM[i-1][j]=PM[i-1][j-1]-3;
  56. if(PM[i][j]==-2)
  57. PM[i-1][j]=PM[i-1][j-1]*4,PM[i][j]=PM[i-1][j]-1,PM[i][j-1]=PM[i-1][j]-2,PM[i-1][j-1]=PM[i-1][j]-3;
  58. if(PM[i][j]==-3)
  59. PM[i-1][j]=PM[i-1][j-1]*4,PM[i][j-1]=PM[i-1][j]-1,PM[i][j]=PM[i-1][j]-2,PM[i-1][j-1]=PM[i-1][j]-3;
  60. }
  61. for(int i=0;i<n;++i,cout<<'\n')
  62. for(int j=0;j<n;++j)
  63. cout<<PM[i][j]<<' ';
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement