斐波那契数列
兔子数列.png{
Fib(0) = 1
Fib(1) = 1
Fib(n) = Fib(n - 1) + Fib(n - 2)
}
program fibonacci(input, output);
(* ============ GLOBAL VARIABLES ============ *)
var
n : integer;
(* =============== FUNCTIONS ================ *)
function fib(n : longint) : longint;
begin
if n < 2 then
fib := 1
else
fib := fib(n - 1) + fib(n - 2)
end;
function fastfib(n : longint) : longint;
(* ============ LOCAL VARIABLES ============ *)
var
first, second, count, temp : longint;
begin
first := 1;
second := 1;
for count := 2 to n do
begin
temp := second;
second := first + second;
first := temp
end;
fastfib := second;
end;
(* ================ PROGRAM ================= *)
begin
write('n: ');
read(n);
writeln('With fast fib: ', fastfib(n));
writeln('With exponential one: ', fib(n));
end.
温馨提示:不要复制粘贴,你需要的练习是:自己动手慢慢敲代码
一些有用的链接:
CP Pascal Editor下载链接
Free Pascal下载链接
那些初高中甚至小学就接触编程的人最后到了什么样的境界?
高中生如何学习编程?
2015年4月21日01:57:17 @kaikai
网友评论