factorial c代码实现

来源:互联网 发布:centos7编译安装mysql 编辑:程序博客网 时间:2024/06/14 22:37
#include <stdio.h>#include <stdlib.h>#include <sys/time.h>double facl(int n){    if (n == 1 || n == 0)        return 1;    return n * facl(n - 1);}double sum(int n){ double a=0;if(n > 0)return a = facl(n) + sum(n-1);elsereturn 0;}int main(){    int n ;    int i,j ;    double answer = 0.0;struct timeval start;struct timeval end;unsigned long diff;    while(1){        printf("please input a number >0...\n");        scanf("%d", &n);        gettimeofday(&start, NULL);        for (i = 0; i < 50000; i++){             answer = sum(n);        }        gettimeofday(&end, NULL);        printf("%.1lf\n",answer);                diff = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;        printf("the difference is %ld  us\n", diff);    }}

原创粉丝点击