SHARE
TWEET

PatratMagic3_of

a guest Jan 26th, 2020 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n;
  6. int a[101][101];
  7.  
  8. void gen_impar(int n, int a[][101]){
  9.     int c;
  10.     c = (n*n*n + n)/2;
  11.     int nr = 1;
  12.     /// general
  13.     for(int x = 0, y = n-1;x<n;++x, --y)
  14.         for(int i=x, j=y;i<x+n;++i, ++j)
  15.             a[i][j] = nr++;
  16.     /// N
  17.     for(int x=0, y=n-1;x<n/2;++x, --y)
  18.         for(int j=y, cnt=0;cnt<=x;j+=2, ++cnt)
  19.             a[x+n][j] = a[x][j], a[x][j]=0; /// sau swap
  20.     /// S
  21.     for(int x=2*(n-1), y=n-1, ln = 0;x>=n+n/2;--x, --y, ++ln)
  22.         for(int j=y, cnt=0;cnt<=ln;j+=2, ++cnt)
  23.             a[x-n][j] = a[x][j], a[x][j]=0;
  24.     /// V
  25.     for(int x = n-1, y = 0;y<n/2;--x, ++y)
  26.         for(int i=x, cnt=0; cnt<=y; i+=2, ++cnt)
  27.             a[i][y+n] = a[i][y], a[i][y] = 0;
  28.     /// E
  29.     for(int x = n-1, y = 2*(n-1), ln = 0;y>=n+n/2;--x, --y, ++ln)
  30.         for(int i=x, cnt=0; cnt<=ln; i+=2, ++cnt)
  31.             a[i][y-n] = a[i][y], a[i][y] = 0;
  32.     /// transpunere din (n/2, n/2) in (0, 0)
  33.     for(int i=0;i<n;++i)
  34.         for(int j=0;j<n;++j)
  35.             a[i][j] = a[i+n/2][j+n/2];
  36. }
  37.  
  38. int main()
  39. {
  40.     int b[101][101]; /// indici
  41.     cin>>n;
  42.     gen_impar(n/2, b);
  43.  
  44.     /// generam matricea tip
  45.     char tip[51][51];
  46.     int k = n/4+1;
  47.     for(int i=0;i<k;++i)
  48.         for(int j=0;j<n/2;++j)
  49.             tip[i][j] = 'L';
  50.     for(int j=0;j<n/2;++j)
  51.         tip[k][j] = 'U';
  52.     for(int i=k+1;i<n/2;++i)
  53.         for(int j=0;j<n/2;++j)
  54.             tip[i][j] = 'X';
  55.     swap(tip[n/4][n/4], tip[n/4+1][n/4]);
  56.  
  57.     /// generam matricea rezultat
  58.     for(int i=0, x=0;i<n;i+=2, ++x)
  59.         for(int j=0, y=0;j<n;j+=2, ++y)
  60.     {
  61.         if(tip[x][y] == 'L')
  62.         {
  63.             a[i][j] = 4*b[x][y];
  64.             a[i+1][j+1] = 4*b[x][y]-1;
  65.             a[i+1][j] = 4*b[x][y]-2;
  66.             a[i][j+1] = 4*b[x][y]-3;
  67.         }
  68.         else if(tip[x][y] == 'U')
  69.         {
  70.             a[i][j] = 4*b[x][y]-3;
  71.             a[i+1][j+1] = 4*b[x][y]-1;
  72.             a[i+1][j] = 4*b[x][y]-2;
  73.             a[i][j+1] = 4*b[x][y];
  74.         }
  75.         else if(tip[x][y] == 'X')
  76.         {
  77.             a[i][j] = 4*b[x][y]-3;
  78.             a[i+1][j+1] = 4*b[x][y]-2;
  79.             a[i+1][j] = 4*b[x][y]-1;
  80.             a[i][j+1] = 4*b[x][y];
  81.         }
  82.     }
  83.     for(int i=0;i<n;++i, cout<<'\n')
  84.         for(int j=0;j<n;++j)
  85.             cout<<a[i][j]<<' ';
  86.     return 0;
  87. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top