Advertisement
K1R1LL

Spiral

Nov 22nd, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. //This Code Draw a Spiral
  2. #include<iostream>
  3. #include<vector>
  4. #include<map>
  5. #include<algorithm>
  6. #include<clocale>
  7. #include<cmath>
  8.  
  9. using namespace std;
  10.  
  11. int a[200][200];
  12.  
  13. int main()
  14. {
  15.     ios_base::sync_with_stdio(false);
  16.     cin.tie(0); cout.tie(0);
  17.     int n;
  18.     cin >> n;
  19.     int i = 2;
  20.     int x = (n + 1) / 2, y = x;
  21.     a[x][x] = 1;
  22.     x++;
  23.     while(i < n*n)
  24.     {
  25.         do
  26.         {
  27.             a[y][x] = i;
  28.             x++;
  29.             i++;
  30.         }
  31.         while(a[y - 1][x - 1] > 0);
  32.        
  33.         x--;
  34.         y--;
  35.        
  36.         do
  37.         {
  38.             a[y][x] = i;
  39.             y--;
  40.             i++;
  41.         }while(a[y + 1][x - 1] > 0);
  42.        
  43.         y++;
  44.         x--;
  45.        
  46.         do
  47.         {
  48.             a[y][x] = i;
  49.             x--;
  50.             i++;
  51.         }while(a[y+1][x+1] > 0);
  52.        
  53.         x++;
  54.         y++;
  55.        
  56.         do
  57.         {
  58.             a[y][x] = i;
  59.             y++;
  60.             i++;
  61.         }while(a[y - 1][x+ 1] > 0);
  62.        
  63.         y--;
  64.         x++;
  65.     }
  66.     for(int k = 1; k <= n; k++)
  67.     {
  68.         for(int j = 1; j <= n; j++)
  69.             cout << a[k][j] <<' ';
  70.         cout << "\n";
  71.     }
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement