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程式設計求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 1 x ...
Python程式設計序,用python編寫程式?
python中九九乘法口訣表可以用行和列相乘的方法實現,具體 如下 for i in range 1,10 設定行的數量for j in range 1,i 1 設定列的數量,第幾行就輸出幾列 print s s s j,i,i j end 用end 引數控制換行 print 消除end 引數 py...
python程式設計測試中出現cant multiply se
型別錯誤,檢查運算元型別吧,你沒有給出錯誤的 不好判定.例如print 1 3.5 就會出現 can t multiply sequence by non int of type float 原因是版字串的乘法只權支援int型別 3.5個字串是神馬東東 這個是資料約束丟擲的錯誤 python為什麼會...