Created
December 5, 2016 17:35
-
-
Save s7etozar/d02d226d003b80597f5d5bfac21ab859 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
Функция, выводящая числовые спирали. | |
Например: | |
1 2 3 4 5 | |
18 19 20 21 6 | |
17 28 29 22 7 | |
16 27 30 23 8 | |
15 26 25 24 9 | |
14 13 12 11 10 | |
Входные данные: ширина и высота спирали. | |
*/ | |
printSpiral(17, 15); | |
printSpiral(13, 11); | |
printSpiral(5, 5); | |
printSpiral(18, 7); | |
function printSpiral(width, height) { | |
if (width > 0 && height > 0 && parseInt(width) == width && parseInt(height) == height) { | |
for (var spArray = [], ob, i = 1, j = 0, k = 0, n = 1, q = width * height; n <= q; i++, j++, k += 2) { | |
for (ob = width * j + j, m = 1; m < width - k + 1 && n <= q; ob++, m++, n++) spArray[ob] = n; | |
for (ob = width * (i + 1) - i, m = 1; m < height - k && n <= q; ob += width, m++, n++) spArray[ob] = n; | |
for (ob = width * (height - j) - i - 1, m = 1; m < width - k && n <= q; ob--, m++, n++) spArray[ob] = n; | |
for (ob = q - width * (j + 2) + j, m = 1; m < height - k - 1 && n <= q; ob -= width, m++, n++) spArray[ob] = n; | |
} | |
document.write('<table style=\'text-align: right\'>'); | |
for (var ob = 0, y = 1; y <= height; y++) { | |
document.write('<tr>'); | |
for (var x = 1; x <= width; ob++, x++) document.write('<td style=\'padding: 10px 0 0 13px\'>' + spArray[ob] + '</td>'); | |
document.write('</tr>'); | |
} | |
document.write('</table>'); | |
} else return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment