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程式設計題
我用的是python3.6.x 剛才一個題和這個一樣是你問的嗎? def same first name name1,name2 if name1 is none or name2 is none print name1 or name2 is none elif type name1 type o...
用python程式設計求1 ,用python程式設計求1 1 2 1 3 1 4 1 5 累加和,資料項小於0 1時停止
樹不開叉 def summa summ 0 for i in range 1,100 fl 1.0 i if fl 0.1 summ fl else print the sum is summreturn sum summa 注意縮排 def iteritem limit x 1.while 1 v...
Python簡單程式設計,什麼是python程式設計
def getnumofblocks layer return sum sum range 1,k 1 for k in range 1,layer 1 使用 傳入 layer 引數print getnumofblocks 4 輸出 注意 return 行 要縮排!安裝python直譯器。大多數使用...