Sha256: 568973c22ef0928ba84f160a5a53c59ab072785f91ba303eec3ea48a2a784f58
Contents?: true
Size: 1.02 KB
Versions: 168
Compression:
Stored size: 1.02 KB
Contents
public static class SpiralMatrix { public static int[,] GetMatrix(int size) { var spiral = new int[size, size]; int numbersToPlace = size * size; int currentSpiralValue = 1; int firstPivot = 0, secondPivot = size - 1; while (currentSpiralValue <= numbersToPlace) { for (int i = firstPivot; i <= secondPivot; i++) { spiral[firstPivot, i] = currentSpiralValue++; } for (int j = firstPivot + 1; j <= secondPivot; j++) { spiral[j, secondPivot] = currentSpiralValue++; } for (int i = secondPivot - 1; i >= firstPivot; i--) { spiral[secondPivot, i] = currentSpiralValue++; } for (int j = secondPivot - 1; j >= firstPivot + 1; j--) { spiral[j, firstPivot] = currentSpiralValue++; } firstPivot++; secondPivot--; } return spiral; } }
Version data entries
168 entries across 168 versions & 1 rubygems