Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define N 100
- using namespace std;
- int PM[N][N];
- void GenPmi(int n)
- {
- int i=n/2;
- int j=n-1;
- for(int num=1;num<=n*n;)
- {
- if(i==-1&&j==n)
- j=n-2,i=0;
- else
- {
- if(j==n)
- j=0;
- if(i<0)
- i=n-1;
- }
- if(PM[i*2][j*2])
- {
- j-=2;
- ++i;
- continue;
- }
- else
- PM[i*2][j*2]=num++;
- ++j;--i;
- }
- }
- int main()
- {
- int n;
- cin>>n;
- /// Pas 1 (L=-1,U=-2,X=-3)
- for(int i=1;i<=n/2;i+=2)
- for(int j=1;j<n;j+=2)
- PM[i][j]=-1;
- for(int j=1;j<n;j+=2)
- PM[n/2+2][j]=-2;
- for(int i=n/2+4;i<n;i+=2)
- for(int j=1;j<n;j+=2)
- PM[i][j]=-3;
- /// Pas 2
- swap(PM[n/2][n/2],PM[n/2+2][n/2]);
- /// Pas 3
- GenPmi(n/2); /// Generez patratul magic impar 2k+1
- /// Pas 4
- for(int i=1;i<n;i+=2)
- for(int j=1;j<n;j+=2)
- {
- if(PM[i][j]==-1)
- 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;
- if(PM[i][j]==-2)
- 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;
- if(PM[i][j]==-3)
- 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;
- }
- for(int i=0;i<n;++i,cout<<'\n')
- for(int j=0;j<n;++j)
- cout<<PM[i][j]<<' ';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement