1. 如何實作楊輝三角
import copy
list=[]
newlist=[]
def Fibonacci(list,n):
newlist.append(0)
if n ==1:
return [1]
for i in range(n):
if i==0 or i==n-1:
newlist[i]=1
else:
newlist[i]=list[i-1]+list[i]
return newlist
blanknum=68
for i in range(17):
blanknum = blanknum - 4
list=copy.deepcopy(Fibonacci(list,i+1))
for i in range(blanknum/2):
print " ",
for i in list:
print str(i).ljust(6),
print ""
實作的效果如下:
看上去還是有點沒對齊。。。
2. 如何實作心
def printlove(start,length,midnumber=0,flag=31):
for i in range(31):
if i<start or i>start+length-1 and i<15-(midnumber-1)/2 or i>15+(midnumber-1)/2 and i<31-start-length or i> 30-start or i==flag:
print " ",
else:
print "*",
print ""
for i in range(16):
if i ==0:
printlove(4,3)
elif i==1:
printlove(1,9)
elif i>=2 and i<=5:
printlove(0,i+10)
elif i==6:
printlove(1,7,7,15)
elif i>=7 and i<=8:
printlove(i-5,6,5-(i-7)*2)
elif i==9:
printlove(5,6,1)
elif i==10:
printlove(8,6,1)
elif i==15:
printlove(15,1,1)
else:
printlove(i-1,16-i,1)
這樣方式有點類似于窮舉法,其實這個圖案的原始實作算法是根據正弦函數來實作的~具體可參考網上其它語言的實作。