#include <time.h>
void hannota(int n,char ta1,char ta2,char ta3)
void main()
{
int n
clock_t begin, duration
printf("input the number of diskes:")
scanf("%d",&n)
begin = clock()
hannota(n,'1','2','3')
duration = clock() - begin
printf( "用时约: %d毫秒", duration*1000 / CLOCKS_PER_SEC )
printf("\n")
}
void hannota(int n,char ta1,char ta2,char ta3)
{
if(n==1)
printf("%c--->%c",ta1 ,ta2)
else
{
hannota(n-1,ta1,ta3,ta2)
printf("%c---->%c",ta1,ta2)
hannota(n-1,ta3,ta2,ta1)
}
}
C/C++中的计时函数是clock()。所以,可以用clock函数来计算的运行一个循环、程序或者处理其它事件到底花了多少时间,具体参考代码如下:
#include “stdio.h”
#include “stdlib.h”
#include “time.h”
int main( void )
{
longi = 10000000L
clock_t start, finish
double duration
/* 测量一个事件持续的时间*/
printf( "Time to do %ld empty loops is ", i )
start = clock()
while( i-- )
finish = clock()
duration = (double)(finish - start) / CLOCKS_PER_SEC
printf( "%f seconds\n", duration )
system("pause")
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)