鱼C论坛

 找回密码
 立即注册
查看: 1562|回复: 3

[已解决]电脑算不出来了 求好心人帮我算两个时间

[复制链接]
发表于 2021-4-9 22:55:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码直接复制应该就没问题 如果算不出来可以把第五行#define maxkt的数值改小
求一下输出以后的两个时间 谢谢谢谢!

#include <stdio.h>
#include <time.h>
#include <math.h>
#define ArraySize 1000001
#define maxkt 100

/*run this program using the console pauser or
add your own getch,system("pause") or input loop*/

double f1(double x,double a[],int n) //累计求和
{
        int i;
        double p=0;
        for(i=0;i<=n;i++)
                p+=a[i]*pow(x,i);
        return p;
}

double f2(double x,double a[],int n)   //乘法结合律
{
        int i;
        double p=a[n];
        for(i=n;i>0;i--)
        {p=a[i-1]+x*p;}
        return p;
}

double f1(double x, double a[],int n);
double f2(double x, double a[],int n);

clock_t start,end;
int main()
{
        double a[ArraySize];
        int i;
        double j;
        for(i=1;i<ArraySize;i++)
        {j=i;
        a[i]=1/j;}
        a[0]=1;          //引入a[]

        start = clock();
        for (i=0;i<maxkt;i++)
                f1(1.2,a,ArraySize-1);
        end=clock();
        double duration1 = (double)(end-start)/CLK_TCK/maxkt;
        printf("累计求和所用时间:%e\n",duration1);
        double t;
        t=f1(1.2,a,ArraySize-1);
        printf("累计求和结果=%lf\n",t);
       
        start = clock();
        for (i=0;i<maxkt;i++)
                f2(1.2,a,ArraySize-1);
        end=clock();
        double duration2 = (double)(end-start)/CLK_TCK/maxkt;
        printf("乘法结合律所用时间:%e\n",duration2);
        double y;
        f2(1.2,a,ArraySize-1);
        y=f2(1.2,a,ArraySize-1);
        printf("累计求和结果=%lf\n",y);
        return 0;
}
最佳答案
2021-4-9 23:39:34
顺带一提,你那个maxkt怎么改都没有用,因为 1 /  1000000 本身就溢出了....
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-9 23:25:10 | 显示全部楼层
本帖最后由 yuxijian2020 于 2021-4-9 23:40 编辑

你这写的....我稍微给你改了下

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include <string.h>

  6. #define ArraySize 1000001
  7. #define maxkt 100

  8. /*run this program using the console pauser or
  9. add your own getch,system("pause") or input loop*/

  10. double f1(double x, double a[], int n) //累计求和
  11. {
  12.     int i;
  13.     long double p = 0;
  14.     for (i = 0; i <= n; i++)
  15.         p += a[i] * pow(x, i);
  16.     return p;
  17. }

  18. double f2(double x, double a[], int n)   //乘法结合律
  19. {
  20.     int i;
  21.     long double p = a[n];
  22.     for (i = n; i > 0; i--)
  23.     {
  24.         p = a[i - 1] + x * p;
  25.     }
  26.     return p;
  27. }

  28. double f1(double x, double a[], int n);
  29. double f2(double x, double a[], int n);

  30. clock_t start, end;
  31. int main()
  32. {
  33.     //double a[ArraySize];
  34.     double* a = (double*)malloc(ArraySize * sizeof(double));
  35.     if (a == NULL)
  36.     {
  37.         printf_s("空间申请失败...\n");
  38.         return -1;
  39.     }
  40.     memset(a, 0, ArraySize * sizeof(double));

  41.     int i;
  42.     double j;
  43.     for (i = 1; i < ArraySize; i++)
  44.     {
  45.         j = i;
  46.         a[i] = 1 / j;
  47.     }
  48.     a[0] = 1;          //引入a[]

  49.     start = clock();
  50.     for (i = 0; i < maxkt; i++)
  51.         f1(1.2, a, ArraySize - 1);
  52.     end = clock();
  53.     double duration1 = (double)end - (double)start / CLK_TCK / maxkt;
  54.     printf("累计求和所用时间:%.2lf 毫秒\n", duration1);
  55.     long double t;
  56.     t = f1(1.2, a, ArraySize - 1);
  57.     printf("累计求和结果=%llf\n", t);

  58.     start = clock();
  59.     for (i = 0; i < maxkt; i++)
  60.         f2(1.2, a, ArraySize - 1);
  61.     end = clock();
  62.     double duration2 = (double)end - (double)start / CLK_TCK / maxkt;
  63.     printf("乘法结合律所用时间:%.2lf 毫秒\n", duration2);
  64.     long double y;
  65.     //f2(1.2, a, ArraySize - 1);
  66.     y = f2(1.2, a, ArraySize - 1);
  67.     printf("累计求和结果=%llf\n", y);

  68.     free(a);
  69.     return 0;
  70. }
复制代码


111.png
结果你就不要想了,我用了long double都还是溢出....
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-9 23:39:34 | 显示全部楼层    本楼为最佳答案   
顺带一提,你那个maxkt怎么改都没有用,因为 1 /  1000000 本身就溢出了....
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-10 08:48:07 | 显示全部楼层
yuxijian2020 发表于 2021-4-9 23:39
顺带一提,你那个maxkt怎么改都没有用,因为 1 /  1000000 本身就溢出了....

好的 感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-10 12:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表