python程式設計2 1 3 2 5 3 8 5 13 8前50項和

2021-06-28 17:14:15 字數 1552 閱讀 7751

1樓:匿名使用者

x=1.0;y=2.0;ret=0

for i in range(50):

ret+=y/x

t=yy+=x

x=tprint ret

2樓:匿名使用者

well, 'cause i noticed that some of the number can't be divided exactly,

so i thought keeping the result as  a fraction would be more accurate.

but after done writing, i found it's not that valuable to use fraction,

the number would get huge because there's not manycommon factors between the nominator and denominator.

my code is here, just for reference~

分別用非遞迴和遞迴的方法編寫函式求斐波那契數列第n項。斐波那契數列1,1,2,3,5,8,13,…

3樓:墨汁諾

/**已知fibonacci數列:1,1,2,3,5,8,……,f(1)=1,f(2)=1,f(n)=f(n-1)+f(n-2)

*/#include

#include

typedef long long int int64;

//方法1,遞迴法

int64 fibonacci(int n)

if(n==1 || n==2)

return 1;

else

sum=fibonacci(n-1)+fibonacci(n-2);

return sum;

}非遞迴法

int64 fibonacci2(int n)

if(n==1 || n==2)

return 1;

a=b=1;  //對前兩項的值初始化

n=n-2;  //因為是從第3項開始記次數,所以減2

while(n > 0)

return c;

}//測試主函式

int main()

//示例執行結果

f:\c_work>a.exe

5f(5)=5

f:\c_work>a.exe

6f(6)=8

program fibo;var n,i:integer; rs:extended;function fib(m:

integer):extended;var a,b:extended;

begin

a:=1;b:=1;if m<=2 then exit(1)else while m>3 do begin

fib:=a+b;a:=b;b:=fib;m:=m-1;end;exit(fib);end;

begin

read(n);writeln(fib(n));end.

python程式設計題,求解答謝謝,Python程式設計題,求解答

如果確bai定du兩個zhi 列表長度 dao一專樣的屬話 python程式設計題,求解答 coding utf 8 import copy 初始化menu1字典,輸入兩道菜的 menu1 menu1 fish int input menu1 pork int input menu total列表現...

關於Python程式設計的問題,關於Python程式設計的問題

這種情況,要切換一下碟符 輸入e 即可,並且路徑已經是work了 這個是dos的基礎,與python倒是無關,不過會困擾你 cd空格 d e work 不會用dos命令可在命令列輸入 cd 檢視 你輸入 e 試試。看看能不能到e盤。關於python程式設計的問題 請求幫助 python 指令碼名稱....

python程式設計題目。怎麼做阿大神

假設人員關係表中,相關人員 中的名稱不重複,並且所有人名除非第一次出現,其它情況只出現在 人員 列 即,排除反向相關 如果不排除反向相關,則做一些修改 你可以使用廣度搜尋演算法來分類 有沒有python程式設計大神幫忙看一下這個題怎麼做?說得好 python 這個題目應該怎麼做啊,跪求大神解答!de...