返回
关于我们
分类

算法效率分析

日期: 2020-01-06 14:20 浏览次数 : 85

# include <stdio.h>
#滚球体育, include <sys/timeb.h>

int main(void)
{
    timeb t1, t2;
    long t;
    int i, n;
    double x, sum1, sum;

    sum1 = 1;
    sum = 1;

    printf("Please input x and n: ");
    scanf("%lf%d", &x, &n);
    ftime(&t1); //求的当前时间

    for (i=1; i<=n; ++i)
    {
        sum1 *= -1.0 / x;
        sum += sum1;
    }
    ftime(&t2);//求的当前时间

    t = (t2.time - t1.time)*1000 + (t2.millitm - t1.millitm);//时间差
    printf("sum = %lf 用时%ld毫秒n", sum, t);

    return 0;
}

虽不是自己写的但是在自己理解的基础上还是把它写出来了