[code=C/C++][/code]#include<cmath>
#include<ctime>
#include<iostream>
#include<time.h>
using namespace std;
int fib(int n){
int fib=0;
int i;
int f1=1;
int f2=1;
if(n==1 || n==2)
return 1;
else{
for(i = 3; i <= n; i++){
fib = f1 + f2;
f1 = f2;
f2 = fib;
}
}
return fib;
}
int main(){
int N,result;
clock_t start,end;
cout << "Enter a Number: ";
cin >> N;
start = clock();
result = fib(N);
int duration =(int) (end - start) / CLOCKS_PER_SEC ;
cout << "The fib number is : " << result << endl;
cout << "The program takes "<< duration << endl;
//system("Pause");
return 0;
}
得出的运行时间为-1073,即使换N值也是一样的时间-1073,在linux环境下运行的,用的是putty.
请问怎么改?精确到毫秒的话是不是在CLOCLS_PER_SEC 后面*1000?谢谢 |