Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main()
- {
- int n = 0, m = 0;
- cin >> n;
- m = n;
- int HelpN = n, HelpM = m;
- int A[n][m];
- int sum = n * m;
- int Y = 0, X = 0;
- int counter = 1;
- for (int i = 0 ; i < n; i++)
- for (int j = 0 ; j < m; j++)
- A[i][j] = 0;
- while (n > 0)
- {
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < max(m,n); j++)
- {
- if (i == 0 && j < m - X && counter <= sum)
- A[i + Y][j + X] = counter++;
- if (i == 1 && j < n - Y && j != 0 && counter <= sum)
- A[j + Y][m - 1] = counter++;
- if (i == 2 && j < m - X && j != 0 && counter <= sum)
- A[n - 1][m - (j + 1)] = counter++;
- if (i == 3 && j < n - (Y + 1)&& j != 0 && counter <= sum)
- A[n - (j + 1)][Y] = counter++;
- }
- }
- n--; m--;
- Y += 1; X += 1;
- }
- for (int i = 0; i < HelpN; i++)
- {
- for (int j = 0; j < HelpM; j++)
- printf("%4d", A[i][j]);
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement