Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. int Arr[255][255];
  2.     int n = 5;
  3.     int iCount = 1;
  4.     Arr[0][0] = iCount++;
  5.  
  6.     int i = 0, j = 1;
  7.     int dx = 1, dy = -1;
  8.  
  9.     while (iCount < n*n)
  10.     {
  11.         Arr[i][j] = iCount++;
  12.        
  13.         i += dx;
  14.         j += dy;
  15.  
  16.         //Xu ly cham bien
  17.         if (j == -1 || i  >= n)
  18.         {
  19.             if (i <= n - 1)
  20.             {
  21.                 j = 0;
  22.                
  23.             }
  24.             else
  25.             {
  26.                 i = n - 1;
  27.                 j += 2;
  28.                
  29.             }
  30.             dx = -dx;
  31.             dy = -dy;
  32.            
  33.         }
  34.  
  35.         else if (i == -1 || j >= n)
  36.         {
  37.             if (j <= n - 1)
  38.             {
  39.                 i = 0;
  40.                
  41.             }
  42.             else
  43.             {
  44.                 j = n - 1;
  45.                 i += 2;
  46.            
  47.             }
  48.             dx = -dx;
  49.             dy = -dy;
  50.            
  51.         }
  52.  
  53.        
  54.  
  55.  
  56.  
  57.  
  58.        
  59.     }
  60.  
  61.     Arr[n - 1][n - 1] = iCount;
  62.  
  63.     for (int i = 0; i < n; i++)
  64.     {
  65.         for (int j = 0; j < n; j++)
  66.         {
  67.             cout << Arr[i][j] << "      ";
  68.         }
  69.         cout << endl << endl << endl;
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement