자바 10, 284 282 281 263 바이트
s->n->{var c=new char[s][s];for(var d:c)java.util.Arrays.fill(d,' ');for(int i=0,j=0,y=0,x=1,u=s-1,l=0;n-->0;c[j][i]=42,i+=x,j+=y,l+=i==l&x==0?1:0,u-=i==l&j==l&y<1?1:0)if(x!=0){var b=x>0?i<u:i>l;y=b?0:x;x=b?x:0;}else{var b=y>0?j<u:j>l;x=b?0:-y;y=b?y:0;}return c;}
재미있는 도전!
여기에서 온라인으로 사용해보십시오 .
18 바이트 골프 를 한 Kevin Cruijssen 에게 감사 합니다.
언 골프 버전 :
s -> n -> { // lambda taking two integer arguments in currying syntax
var c = new char[s][s]; // the matrix containing the spiral
for(var d : c) // for every row
java.util.Arrays.fill(d, ' '); // fill it with spaces
for(int i = 0, j = 0, // the coordinates of the next '*'
y = 0, x = 1, // the direction to move in
u = s-1, l = 0; // the upper and lower bounds
n-- > 0; // decrecement the length of the spiral and repeat as many times
c[j][i] = 42, // draw the '*', 42 is ASCII code
i += x, j += y, // move to the next cell
l += i == l & x == 0 ? 1 : 0, // adjust lower bound if necessary
u -= i == l & j == l & y < 1 ? 1 : 0) // adjust upper bound if necessary
if(x != 0) { // if moving in x direction
var b = x > 0 ? i < u : i > l; // if we hit the bounds
y = b ? 0 : x; // flip directions,
x = b ? x : 0; // turning around
} else { // if moving in y direction
var b = y > 0 ? j < u : j > l; // if we hit the bounds
x = b ? 0 : -y; // flip directions,
y = b ? y : 0; // turning around
}
return c; // return the matrix
}