# 斐波那契螺旋

37

### 输入/输出示例

``````1 -> 1

2 -> 1 1

3 -> 1 1
2 2
2 2

6 -> 8 8 8 8 8 8 8 8 5 5 5 5 5
8 8 8 8 8 8 8 8 5 5 5 5 5
8 8 8 8 8 8 8 8 5 5 5 5 5
8 8 8 8 8 8 8 8 5 5 5 5 5
8 8 8 8 8 8 8 8 5 5 5 5 5
8 8 8 8 8 8 8 8 1 1 3 3 3
8 8 8 8 8 8 8 8 2 2 3 3 3
8 8 8 8 8 8 8 8 2 2 3 3 3
``````

``````1   1                          1   1
100 100  should actually be  100 100
``````

4

Sp3000

`Numbers with different amounts of digits (e.g. 1 and 13) should be aligned to the left side of the digit a space may need to be added to the very beginning of a line so everything can line up.`听起来好像两个句子可能更清晰。
trichoplax

trichoplax

MtnViewMark

Matlab默认将输出打印到标准输出。将数字类型的输出（而不是字符串类型的输出）自动打印到stdout是否可以接受？

Answers:

15

## APL，23

``````{a,⍴⍨2⍴⊃⍴a←⌽⍉⍵}⍣(⎕-1)⍪1
``````

``````⍪1               this creates a 1x1 matrix containing just 1
{..}⍣(⎕-1)     the power operator (⍣) repeats the function {} user input - 1 times
a,⍴⍨2⍴⊃⍴a←⌽⍉⍵   the function being iterated rotates the matrix and appends the next matrix to it.
``````

1

gar 2015年

1
@MorisZucca但是请注意，该字符集中缺少某些字符（如`⌸``⍤`），并且在您要调用该规则时无法使用。
FUZxxl

1

8

# Matlab，84个字节

``````function f(N)
s=0;t=1;y=1;for n=2:N
u=s+t;s=t;t=u;y=[rot90(y) t*ones(t)];end;disp(y)
``````

``````>> f(1)
1
>> f(2)
1     1
>> f(3)
1     2     2
1     2     2
>> f(6)
5     5     5     5     5     8     8     8     8     8     8     8     8
5     5     5     5     5     8     8     8     8     8     8     8     8
5     5     5     5     5     8     8     8     8     8     8     8     8
5     5     5     5     5     8     8     8     8     8     8     8     8
5     5     5     5     5     8     8     8     8     8     8     8     8
3     3     3     1     1     8     8     8     8     8     8     8     8
3     3     3     2     2     8     8     8     8     8     8     8     8
3     3     3     2     2     8     8     8     8     8     8     8     8
>> f(7)
8     8     8     8     8     8     8     8    13    13    13    13    13    13    13    13    13    13    13    13    13
8     8     8     8     8     8     8     8    13    13    13    13    13    13    13    13    13    13    13    13    13
8     8     8     8     8     8     8     8    13    13    13    13    13    13    13    13    13    13    13    13    13
8     8     8     8     8     8     8     8    13    13    13    13    13    13    13    13    13    13    13    13    13
8     8     8     8     8     8     8     8    13    13    13    13    13    13    13    13    13    13    13    13    13
8     8     8     8     8     8     8     8    13    13    13    13    13    13    13    13    13    13    13    13    13
8     8     8     8     8     8     8     8    13    13    13    13    13    13    13    13    13    13    13    13    13
8     8     8     8     8     8     8     8    13    13    13    13    13    13    13    13    13    13    13    13    13
5     5     5     5     5     1     2     2    13    13    13    13    13    13    13    13    13    13    13    13    13
5     5     5     5     5     1     2     2    13    13    13    13    13    13    13    13    13    13    13    13    13
5     5     5     5     5     3     3     3    13    13    13    13    13    13    13    13    13    13    13    13    13
5     5     5     5     5     3     3     3    13    13    13    13    13    13    13    13    13    13    13    13    13
5     5     5     5     5     3     3     3    13    13    13    13    13    13    13    13    13    13    13    13    13
``````

## Matlab，78个字节

``````function y=f(N)
s=0;t=1;y=1;for n=2:N
u=s+t;s=t;t=u;y=[rot90(y) t*ones(t)];end
``````

``````f(6)
ans =
5     5     5     5     5     8     8     8     8     8     8     8     8
5     5     5     5     5     8     8     8     8     8     8     8     8
5     5     5     5     5     8     8     8     8     8     8     8     8
5     5     5     5     5     8     8     8     8     8     8     8     8
5     5     5     5     5     8     8     8     8     8     8     8     8
3     3     3     1     1     8     8     8     8     8     8     8     8
3     3     3     2     2     8     8     8     8     8     8     8     8
3     3     3     2     2     8     8     8     8     8     8     8     8
``````

Hoki 2015年

@Hoki谢谢！:-)

7

# Python 2，121字节

``````a,b=0,1;L=[]
exec"a,b=b,a+b;L=zip(*L[::-1])+[[a]*a]*a;"*input()
for r in L:print" ".join("%*d"%(len(str(a)),x)for x in r)
``````

7

# 红宝石，243个242 236 233 222 170 130字节

``````s,l,r=0,1,[]
gets.to_i.times{s+=l
l=s-l
s.times{r<<[s]*s}
r=r.transpose.reverse}
r.map{|w|puts w.map{|c|"%#{s.to_s.size}s"%c}*" "}
``````

1

Cristian Lupascu

6

# 蟒蛇- 189 179 174

``````n=int(input())
f=[1,1]
while len(f)<n:f+=[f[-1]+f[-2]]
o=[[]]
for i in f:o=(list(zip(*o)))[::-1]+[[i]*i]*i
for x in o:print(' '.join(str(y).rjust(len(str(f[-1])))for y in x))
``````

6

# J，36个字节

``````1&((\$~,~)@(1{\$@]),.|:@|.@])&(,.1)@<:
``````

``````   (1&((\$~,~)@(1{\$@]),.|:@|.@])&(,.1)@<:) 6
8 8 8 8 8 8 8 8 5 5 5 5 5
8 8 8 8 8 8 8 8 5 5 5 5 5
8 8 8 8 8 8 8 8 5 5 5 5 5
8 8 8 8 8 8 8 8 5 5 5 5 5
8 8 8 8 8 8 8 8 5 5 5 5 5
8 8 8 8 8 8 8 8 1 1 3 3 3
8 8 8 8 8 8 8 8 2 2 3 3 3
8 8 8 8 8 8 8 8 2 2 3 3 3
``````

``````1&(           loop
(\$~,~)      new square with size and elements
@(1{\$@])    with the size of the second dimension of the current rectangle
,.          attached to
|:@|.@]     rotated current rectangle
)&(,.1)       starting the loop with matrix 1
@<:           looping input-1 times
``````

6

## Haskell中，183个176 171 163字节

``````import Data.List
s t=map((t>>[l t])++)t
e 1=[[1]];e n=s.reverse.transpose\$e\$n-1
f=g.e
g m=unlines\$map(>>=((show\$l m)#).show)m
a#b|l a<l b=b;a#b=a#(' ':b)
l=length
``````

``````λ: putStr \$ f 8
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21  5  5  5  5  5  8  8  8  8  8  8  8  8
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21  5  5  5  5  5  8  8  8  8  8  8  8  8
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21  5  5  5  5  5  8  8  8  8  8  8  8  8
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21  5  5  5  5  5  8  8  8  8  8  8  8  8
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21  5  5  5  5  5  8  8  8  8  8  8  8  8
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21  3  3  3  1  1  8  8  8  8  8  8  8  8
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21  3  3  3  2  2  8  8  8  8  8  8  8  8
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21  3  3  3  2  2  8  8  8  8  8  8  8  8
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 13 13 13 13 13 13 13 13 13 13 13 13 13
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 13 13 13 13 13 13 13 13 13 13 13 13 13
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 13 13 13 13 13 13 13 13 13 13 13 13 13
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 13 13 13 13 13 13 13 13 13 13 13 13 13
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 13 13 13 13 13 13 13 13 13 13 13 13 13
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 13 13 13 13 13 13 13 13 13 13 13 13 13
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 13 13 13 13 13 13 13 13 13 13 13 13 13
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 13 13 13 13 13 13 13 13 13 13 13 13 13
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 13 13 13 13 13 13 13 13 13 13 13 13 13
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 13 13 13 13 13 13 13 13 13 13 13 13 13
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 13 13 13 13 13 13 13 13 13 13 13 13 13
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 13 13 13 13 13 13 13 13 13 13 13 13 13
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 13 13 13 13 13 13 13 13 13 13 13 13 13
``````

5

# Pyth，34个字节

``````jbmsm.[hl`lhZ`k\ d=Zu+_CGmmlGGGQ]]
``````

7的示例输出：

``````  5  5  5  5  5  8  8  8  8  8  8  8  8
5  5  5  5  5  8  8  8  8  8  8  8  8
5  5  5  5  5  8  8  8  8  8  8  8  8
5  5  5  5  5  8  8  8  8  8  8  8  8
5  5  5  5  5  8  8  8  8  8  8  8  8
3  3  3  1  1  8  8  8  8  8  8  8  8
3  3  3  2  2  8  8  8  8  8  8  8  8
3  3  3  2  2  8  8  8  8  8  8  8  8
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
``````

4

## Perl，289277257字节

``@f=(0,1);push@f,\$f[-1]+\$f[-2]while(@f<=\$ARGV[0]);\$d=1+length\$f[-1];shift@f;map{\$v=\$f[\$_];\$t=sprintf("%\${d}d",\$v)x\$v;\$_%4||map{unshift@s,\$t}1..\$v;\$_%4==3&&map{\$_.=\$t}@s;\$_%4==2&&map{push@s,\$t}1..\$v;\$_%4==1&&map{\$_=\$t.\$_}@s;}0..\$#f;\$\=\$/;for(@s){s/^ //;print}``

4

# K，48个字节

``````{{`0:1_',/'(1+#\$|//x)\$x}(x-1){+|x,\:t#t:#x}/,,1}
``````

``````  {{`0:1_',/'(1+#\$|//x)\$x}(x-1){+|x,\:t#t:#x}/,,1}7
8  8  8  8  8  8  8  8  5  5  5  5  5
8  8  8  8  8  8  8  8  5  5  5  5  5
8  8  8  8  8  8  8  8  5  5  5  5  5
8  8  8  8  8  8  8  8  5  5  5  5  5
8  8  8  8  8  8  8  8  5  5  5  5  5
8  8  8  8  8  8  8  8  1  1  3  3  3
8  8  8  8  8  8  8  8  2  2  3  3  3
8  8  8  8  8  8  8  8  2  2  3  3  3
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
13 13 13 13 13 13 13 13 13 13 13 13 13
``````

``````  {(x-1){+|x,\:t#t:#x}/,,1}5
(3 3 3 2 2
3 3 3 2 2
3 3 3 1 1
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5)
``````

``````{`0:1_',/'(1+#\$|//x)\$x}
``````

4

# CJam，48个字节

``````1saali({z{W%}%_0=,__sa*a*+}*_W=W=,):U;{USe[}f%N*
``````

``````1s    First value. Using string for values so that we can pad them in the end.
aa    Wrap it twice. Data on stack will be a list of lists (list of lines).
li    Get input.
(     Decrement, since we seeded the list at n=1.
{     Loop over n.
z     Transpose...
{W%}% ... and reverse all lines, resulting in a 90 degree rotation.
_0=,  Get length of line, which is the size of square we need to add.
__    Create two copies of size.
sa    Convert one size to string, and wrap it in array.
*     Replicate it size times. This is one line.
a     Wrap the line...
*     ... and replicate it size times. The square of new values is done.
+     Add the list of lines to the previous list of lines.
}*    End of loop over n.
_W=W= Get last value produced.
,)    Take its length, and increment it. This is the output field width.
:U;   Store the field width in variable, and pop it. This is ugly.
{     Start of block applied to all values.
U     Field width stored in variable.
S     Space.
e[    Pad left.
}f%   End of block applied to all values.
N*    Join lines with newline.
``````

Esolanging Fruit

2

# Pyth，29个字节

``````Vu+C_GmmlGGGQ\]Yjdm.\[l`lN`d\ N
``````

``````u+C_GmmlGGGQ]Y
``````

2

## Ruby，129个字节

``````s,r=0,[[1]]
gets.to_i.times{s+=r[0][0]
r=(r+[[s]*s]*s).transpose.reverse}
r.map{|w|puts w.map{|c|"%#{r[0][s].to_s.size}s"%c}*' '}
``````

1

Martin Ender

user2251284

1

## ES6，248个字节

``````n=>(f=(n,o=n)=>Array(n).fill(o),g=n=>n<3?[f(n,1)]:(a=g(n-2)).reverse().concat(f(l=a[0].length,f(l))).map((e,i,a)=>f(a.length).concat(e.reverse())),a=g(n),s=' '.repeat(l=` \${a[0][0]}`.length),a.map(a=>a.map((e,i)=>(s+e).slice(!i-1)).join``).join`\n`)
``````

`f`是创建填充数组的辅助函数。它主要用于创建实心正方形，但也可以方便地加倍以生成递归的基础案例。

`g`是主要的任务。它递归生成最后一个解决方案，将其旋转180度，然后追加下两个正方形。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.