- 分享
- 0
- 人气
- 0
- 主题
- 9
- 帖子
- 514
- UID
- 55545
- 积分
- 4160
- 阅读权限
- 22
- 注册时间
- 2006-12-24
- 最后登录
- 2017-10-17
- 在线时间
- 4527 小时
|
这是我的功课 : -
1.Write an iterative and a recursive version of the Fibonacci series algorithm. You need to ensure the correctness of the both algorithms. Both algorithms should produce similar output if given a similar input.
a. Measure the performance of the two algorithms by measuring the time for both algorithms to listing out 10, 20, 30, 40, 50, 60, 70 and 80 of Fibonacci numbers. The listing procedure could be done with a loop. For each test, repeat 3 times and get the average value.
Your report should include the following items:-
i.Problem statements
ii.The codes of your both algorithms
iii.Your machines specification, e.g. CPU type and amount of RAM.
iv.A table of your experiment results, before and after the average.
v.A graph of your experiment results using the average.
vi.A short discussion from the results obtained
这是我的coding for iterative
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void){
double num[80];
int n;
srand(time(NULL));
clock_t start;
clock_t stop;
printf("lease enter the size of Fibonacci series : ");
scanf("%d",&n);
start = clock();
for(int i = 0 ;i < n ; i++){
if(i == 0 || i == 1)
num = i;
else
num = num[i-1] + num[i-2];
printf("\n%d",i+1);
printf("\t%.f\n",num);
}
stop = clock();
printf("The time to stop this prosses is %.3f\n",(float)(stop-start)/CLOCKS_PER_SEC);
system("AUSE");
return 0;
}
这是我的coding for iterative
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
double fib(int num);
int main(void){
int n;
srand(time(NULL));
clock_t start;
clock_t stop;
printf("lease the size of Fibonacci series : ");
scanf("%d",&n);
start = clock();
for(int i = 0 ; i < n ; i++){
printf("\n%d",i+1);
printf("\t%.f\n",fib(i));}
stop = clock();
printf("The time to stop this prosses is %.3f\n",(float)(stop-start)/CLOCKS_PER_SEC);
system("AUSE");
return 0;
}
double fib(int num){
if(num == 0 || num == 1)
return num;
else
return fib(num - 1) + fib(num - 2);
}
For recursive,我loop 40的话需要2X秒~
我的朋友他们只用X秒啊!
原以为是电脑spec的问题,但他的spec比我还差。。。
My PC spec-C2DT6400 4GB DDR2 800
Friend PC spec - M Processor 740 1.73Ghz 512MB RAM
高手指点指点~em0017
谢谢~em0008
[ 本帖最后由 凯茹 于 2009-7-11 11:44 PM 编辑 ] |
|