2021/8/81 滴滴筆試
題目描述:
輸入:正整數n
輸出:n*n的迂迴費波那契矩陣
首先寫出蛇形遍歷陣列過程
import numpy as np
#蛇形遍歷二維陣列
n = n =
int(
input()
.strip())
data = np.zeros(
(n,n)
).astype(
int)
top_row = n-
1top_col = n-
1low_row =
0low_col =
0value =
1while low_row<=top_row and low_col<=top_col:
for j in
range
(low_col,top_col+1)
:# print(low_row,j)
data[low_row]
[j]= value
value +=
1 low_row +=
1for j in
range
(low_row,top_row+1)
:# print(i,top_col)
data[j]
[top_col]
= value
value +=
1 top_col -=
1for j in
range
(top_col,low_col-1,
-1):
# print(top_row,j)
data[top_row]
[j]= value
value +=
1 top_row -=
1for j in
range
(top_row,low_row-1,
-1):
# print(i,low_col)
data[j]
[low_col]
= value
value +=
1 low_col +=
1print
(data)
這裡給出最終**:
#蛇形斐波那鍥
n =int
(input()
.strip())
data = np.zeros(
(n,n)
).astype(
int)
top_row = n-
1#行邊界上限
top_col = n-
1#列邊界上限
low_row =
0#行邊界下限
low_col =
0#列邊界下限
last1=
0index_list =
while low_row<=top_row and low_col<=top_col:
for j in
range
(low_col,top_col+1)
:[low_row,j])if
(low_row ==
0and j==0)
: data[low_row]
[j]=
1 last2 =
1continue
# print(low_row,j)
data[low_row]
[j]= last2 + last1
last1 = last2
last2 = data[low_row]
[j] low_row +=
1for j in
range
(low_row,top_row+1)
:[j,top_col]
)# print(i,top_col)
data[j]
[top_col]
= last2 + last1
last1 = last2
last2 = data[j]
[top_col]
top_col -=
1for j in
range
(top_col,low_col-1,
-1):
[top_row,j]
)# print(top_row,j)
data[top_row]
[j]= last2 + last1
last1 = last2
last2 = data[top_row]
[j] top_row -=
1for j in
range
(top_row,low_row-1,
-1):
[j,low_col]
)# print(i,low_col)
data[j]
[low_col]
= last2 + last1
last1 = last2
last2 = data[j]
[low_col]
low_col +=
1#逆序產生結果
for i in
range
(n*n//2)
: a1,b1 = index_list[i]
a2,b2 = index_list[-1
-i] temp = data[a1]
[b1]
data[a1]
[b1]
= data[a2]
[b2]
data[a2]
[b2]
= temp
print
(data)
15 斐波那鍥數列
15.斐波那鍥數列 題目描述 大家都知道斐波那契數列,現在要求輸入乙個整數n,請你輸出斐波那契數列的第n項 從0開始,第0項為0,第1項是1 輸入 4返回值 3分析 1.遞迴思想,呼叫斐波那契數列,重複的自己呼叫自己 2.斐波那契數列就是當n 3的時候,結果為1 大於3的時候為f n 1 f n 2...
每日一題 斐波那鍥數列
2020.10.13開始打卡每日一題 題目一 給定斐波那鍥的前兩項,求第n項斐波那鍥數列的值,因為數字很大,我們對結果mod 1e9 7 題目二 給定整數n,代表台階數,一次可以跨2個或者1個台階,求多少種走法 題目三 假設農場中成熟的母牛每年會生一頭小母牛,並且永遠不會死 就是這樣 第一年,農場只...
斐波那契數列 斐波那契數列python實現
斐波那契數列 fibonacci sequence 又稱 分割數列 因數學家列昂納多 斐波那契 leonardoda fibonacci 以兔子繁殖為例子而引入,故又稱為 兔子數列 指的是這樣乙個數列 1 1 2 3 5 8 13 21 34 在數學上,斐波納契數列以如下被以遞推的方法定義 f 1 ...