Advertisement
MaskerQwQ

回文方阵

Dec 1st, 2022
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int k = 1;
  6.  
  7. const int N = 100;
  8. int a[N][N];
  9. int n;
  10.  
  11. void matrix(int p,int q)
  12. {
  13.  
  14.   if(k==n*n+1) return;
  15.  
  16.   for(int i = q;i<=p;i++) a[i][p] = k++;
  17.   for(int i = p-1;i>=q;i--) a[p][i] = k++;
  18.   for(int i = p-1;i>=q;i--) a[i][q] = k++;
  19.   for(int i = q+1;i<p;i++) a[q][i] = k++;
  20.  
  21.   q++,p--;
  22.  
  23.   matrix(p,q);
  24. }
  25.  
  26. int main()
  27. {
  28.   while(scanf("%d",&n)!=EOF)
  29.   {
  30.     k = 1;
  31.     matrix(n,1);
  32.  
  33.     for(int i = 1;i<=n;i++)
  34.     {
  35.       for(int j = 1;j<=n;j++)
  36.       {
  37.         cout << a[i][j] << " ";
  38.       }
  39.       puts("");
  40.     }
  41.   }
  42.   return 0;
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement