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程式設計題?
typrerror range integer end argument expected,got float.because math.sqrt num returns a float number,but what function range needs is a int,so you may...
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新手剛接觸,rawinput求助我的
你少做一型別轉換。pounds float pounds height float height 另外製表符與空格不能混用,全部要用空格。你可以專從前的 開始,刪屬 除前面所有的空白,然後換成空格。一行一行的換。下面的python 錯誤是怎麼回事,怎麼改?謝謝!指令碼本身沒有錯。你注意下縮排。usr...