Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This Code Draw a Spiral
- #include<iostream>
- #include<vector>
- #include<map>
- #include<algorithm>
- #include<clocale>
- #include<cmath>
- using namespace std;
- int a[200][200];
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0); cout.tie(0);
- int n;
- cin >> n;
- int i = 2;
- int x = (n + 1) / 2, y = x;
- a[x][x] = 1;
- x++;
- while(i < n*n)
- {
- do
- {
- a[y][x] = i;
- x++;
- i++;
- }
- while(a[y - 1][x - 1] > 0);
- x--;
- y--;
- do
- {
- a[y][x] = i;
- y--;
- i++;
- }while(a[y + 1][x - 1] > 0);
- y++;
- x--;
- do
- {
- a[y][x] = i;
- x--;
- i++;
- }while(a[y+1][x+1] > 0);
- x++;
- y++;
- do
- {
- a[y][x] = i;
- y++;
- i++;
- }while(a[y - 1][x+ 1] > 0);
- y--;
- x++;
- }
- for(int k = 1; k <= n; k++)
- {
- for(int j = 1; j <= n; j++)
- cout << a[k][j] <<' ';
- cout << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement