094.求π的近似值

举报
C语言与CPP编程 发表于 2022/05/03 23:32:20 2022/05/03
1.1k+ 0 0
【摘要】 #include<stdio.h>#include<math.h>#include<time.h>#include<stdlib.h>#define N 30000void main(){ double e=0.1,b=0.5,c,d; long int i; ...

      #include<stdio.h>
      #include<math.h>
      #include<time.h>
      #include<stdlib.h>
      #define N 30000
      void main()
      {
        double e=0.1,b=0.5,c,d;
        long int i;                /*i: 正多边形边数*/
        float x,y;
        int c2=0,d2=0;
        clrscr();
        puts("***********************************************************");
        puts("* This program is to calculate PI approximatively *");
        puts("* in two methods. *");
        puts("* One method is Regular Polygon Approximating, *");
        puts("* the other is Random Number Method. *");
        puts("***********************************************************");
        puts("\n >> Result of Regular Polygon Approximating:");
        for(i=6;;i*=2)            /*正多边形边数加倍*/
         {
            d=1.0-sqrt(1.0-b*b);     /*计算圆内接正多边形的边长*/
            b=0.5*sqrt(b*b+d*d);
           if(2*i*b-i*e<1e-15) break;         /*精度达1e-15则停止计算*/
            e=b;      /*保存本次正多边形的边长作为下一次精度控制的依据*/
         }
        printf("---------------------------------------------------------\n");
        printf(" >> pi=%.15lf\n",2*i*b);       /*输出π值和正多边形的边数*/
        printf(" >> The number of edges of required polygon:%ld\n",i);
        printf("---------------------------------------------------------\n");
        randomize();
        while(c2++<=N)
         {
              x=random(101);      /*x:坐标。产生0到100之间共101个的随机数*/
              y=random(101);      /*y:坐标。产生0到100之间共101个的随机数*/
             if(x*x+y*y<=10000)     /*利用圆方程判断点是否落在圆内*/
                  d2++;
         }
        puts("\n >> Result of Random Number Method:");
        printf("---------------------------------------------------------\n");
        printf(" >> pi=%f\n",4.*d2/N);    /*输出求出的π值*/
        printf("---------------------------------------------------------\n");
        puts("\n Press any key to quit...");
        getch();
      }
  
 

文章来源: blog.csdn.net,作者:程序员编程指南,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_41055260/article/details/124558271

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。