Created
September 17, 2018 05:57
-
-
Save afishr/f76e5a199d24f28f4750f6aaefd71a20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Array in a spiral | |
int main() | |
{ | |
int i,j,n,w=3,l=0; // w и l - позволяют ориентироваться внутри массива | |
//w - отступ до дальней стенки, l - отступ от ближней | |
int a[4][4]={5,3,2,7,1,8,4,3,2,6,9,7,5,6,3,2}; //Массив 4 на 4 | |
i=0; | |
j=0; | |
for (n=0;n<16;++n) //Повторяется *кол-во элементов* раз | |
{ | |
cout << a[i][j] <<" "; | |
if ((i==(l+1))&&(j==l)) {w--;l++;} //условие перехода на внутреннюю спираль | |
if ((j==w)&&(i<w)) {i++;continue;} //Движение по спирали вниз | |
if ((j<w)&&(i==l)) {j++;continue;} //Вправо | |
if ((i==w)&&(j>l)) {j--;continue;} //Влевo | |
if ((j==l)&&(i>l)) {i--;continue;} //Вверх | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment