打印

程序运行时间问题

[复制链接]
1010|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
秋天落叶|  楼主 | 2012-6-14 16:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
[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?谢谢

相关帖子

沙发
sinadz| | 2012-6-14 22:04 | 只看该作者
没太看明白,应该用一个时间相关的函数就可以吧

使用特权

评论回复
板凳
firstblood| | 2012-6-18 10:12 | 只看该作者
偶也没太明白的啊,顶起了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:落叶很美

138

主题

3044

帖子

1

粉丝