chillurbrain

9. Заполнение по спирали.

May 21st, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int a[500][500];
  6.     int n, x, y, c = 0, t = 0, q = -1;
  7.     cin >> n;
  8.     x = n;
  9.     y = n;
  10.     a[n][n] = c;
  11.     while (x != -1) {
  12.         for (int i = 0 ; i <= t; i++) {
  13.             c++;
  14.             x += q;
  15.             if (x != -1) {
  16.                 a[x][y] = c;
  17.             }
  18.         }
  19.         for (int i = 0 ; i <= t; i++) {
  20.             c++;
  21.             y += q;
  22.             if (x != -1) {
  23.                 a[x][y] = c;
  24.             }
  25.         }
  26.         q = -q;
  27.         t++;
  28.     }
  29.     for (int i = 0; i < 2*n + 1; i++) {
  30.         for (int j = 0; j < 2*n +1; j++) {
  31.             if (a[i][j] < 10) {
  32.                 cout << "  ";
  33.             }
  34.             else {
  35.                 if (a[i][j] < 100) {
  36.                     cout << " ";
  37.                 }
  38.             }
  39.             cout << a[i][j];
  40.         }
  41.         cout << endl;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment