Advertisement
SergeyPGUTI

8.2.9

Feb 19th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string.h>
  4.  
  5.  
  6. using namespace std;
  7.  
  8.  
  9.  
  10. int main()
  11. {
  12.     int n;
  13.     cin>>n;
  14.     int ** p= new int*[n*2+1];
  15.  
  16.     for (int i=0;i<n*2+1;i++)
  17.     {
  18.         p[i]=new int[n*2+1];
  19.     }
  20.  
  21.     for (int i=0;i<n*2+1;i++)
  22.         for(int j=0;j<n*2+1;j++)
  23.             p[i][j]=0;
  24.  
  25.  
  26.  
  27.     int numberCounter=0;
  28.     for (int step=1,I=n,J=n;step<n*2+1;step++)
  29.     {
  30.         for(int m=0;m<step;m++)
  31.         {
  32.             p[I][J]=numberCounter;
  33.             J++;
  34.             numberCounter++;
  35.         }
  36.         for(int m=0;m<step;m++)
  37.         {
  38.             p[I][J]=numberCounter;
  39.             I++;
  40.             numberCounter++;
  41.         }
  42.         step++;
  43.         for(int m=0;m<step;m++)
  44.         {
  45.             p[I][J]=numberCounter;
  46.             J--;
  47.             numberCounter++;
  48.         }
  49.         for(int m=0;m<step;m++)
  50.         {
  51.             p[I][J]=numberCounter;
  52.             I--;
  53.             numberCounter++;
  54.         }
  55.     }
  56.  
  57.     for(int j=0;j<n*2+1;j++) // заполняем верхнюю строку , которую прошлый цикл не захватывает
  58.     {
  59.         p[0][j]=numberCounter;
  60.         numberCounter++;
  61.     }
  62.  
  63.  
  64.  
  65.  
  66. //    вывод p
  67.     for (int i=0;i<n*2+1;i++)
  68.     {
  69.  
  70.         for (int j=0;j<n*2+1;j++)
  71.             {
  72.                 cout.width(3);
  73.                 cout<<p[i][j]<<" ";
  74.             }
  75.             cout<<endl;
  76.     }
  77.  
  78.  
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement