Advertisement
Malinovsky239

Spiral for Kirill :)

May 9th, 2011
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3.  
  4. #define ct 101
  5.  
  6. int a[ct][ct];
  7.  
  8. int main()
  9. {
  10.  
  11.     freopen("input.txt","r",stdin);
  12.     freopen("output.txt","w",stdout);
  13.  
  14.     int n;
  15.  
  16.     scanf("%d ",&n);
  17.  
  18.     for (int i=1;i<=n;i++)
  19.     {
  20.         for (int j=1;j<=n;j++)
  21.         {
  22.             a[i][j]=0;
  23.         }
  24.     }
  25.  
  26.     /*for (int i=1;i<=n;i++)
  27.     {
  28.     for (int j=1;j<=n;j++)
  29.     {
  30.     printf("%d ",a[i][j]);
  31.  
  32.     }
  33.     printf("\n");
  34.     }*/
  35.  
  36.     int k=0;
  37.  
  38.     int i=1,j=0,cnt=0;
  39.  
  40.     while (cnt<n*n)
  41.     {
  42.         if (k%4==0)
  43.         {
  44.             while (a[i][j+1]==0 && j<n)
  45.             {
  46.                 if (cnt <= n * n) fprintf(stderr, "right: (%d, %d) = %d\n", i, j, cnt);
  47.                 cnt++;
  48.                 j++;
  49.                 a[i][j]=cnt;
  50.             }
  51.             k++;
  52.             continue;
  53.         }
  54.  
  55.         if (k%4==1)
  56.         {
  57.             while (a[i+1][j]==0 && i<n)
  58.             {
  59.                 if (cnt <= n * n) fprintf(stderr, "down: (%d, %d) = %d\n", i, j, cnt);
  60.                 cnt++;
  61.                 i++;
  62.                 a[i][j]=cnt;
  63.             }
  64.             k++;
  65.             continue;  
  66.         }
  67.  
  68.         if (k%4==2)
  69.         {
  70.             while (a[i][j-1]==0 && j>1)
  71.             {
  72.                 if (cnt <= n * n) fprintf(stderr, "left: (%d, %d) = %d", i, j, cnt);
  73.                 cnt++;
  74.                 j--;           
  75.                 a[i][j]=cnt;
  76.             }
  77.             k++;
  78.             continue;
  79.         }
  80.  
  81.         if (k%4==3)
  82.         {
  83.             while (a[i-1][j]==0 && i>1)
  84.             {
  85.                 if (cnt <= n * n) fprintf(stderr, "up: (%d, %d) = %d\n", i, j, cnt);
  86.                 cnt++;
  87.                 i--;
  88.                 a[i][j]=cnt;
  89.             }
  90.             k++;
  91.             continue;
  92.         }
  93.     }
  94.  
  95.     for (int i=1;i<=n;i++)
  96.     {
  97.         for (int j=1;j<=n;j++)
  98.         {
  99.             printf("%d ",a[i][j]);
  100.         }
  101.         printf("\n");
  102.     }
  103.  
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement