Advertisement
ValerianBenchea

Construirea unei matrici in spirala

Oct 4th, 2022 (edited)
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int n, i, k, v=0, a[100][100];
  7.     cin>>n;
  8.     for(k=1; k<=n/2+n%2; k++)
  9.     {
  10.         for(j=k; j<=n-k+1; j++)
  11.             a[k][j]=++v;
  12.         for(i=k+1; i<=n-k+1; i++)
  13.             a[i][n-k+1]=++v;
  14.         for(j=n-k; j>=k; j--)
  15.             a[n-k+1][j]=++v;
  16.         for(i=n-k; i>=k+1; i--)
  17.             a[i][k]=++v;
  18.     }
  19.         for(i=1; i<=n; i++)
  20.         {
  21.             for(j=1; j<=n; j++)
  22.                 cout<<a[i][j]<<" ";
  23.             cout<<endl;
  24.         }
  25.     return 0;
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement