- UID
- 33415
- 在线时间
- 0 小时
- 最后登录
- 2017-9-27
- 注册时间
- 2011-11-24
- 宅魂
- 992 点
- 贡献
- 400 点
- 宅币
- 3207 枚
- 宅の石(入宅度)
- 0 块
- 元气(技能点)
- 39 点
- 活跃
- 0 ℃
- 听众
- 39
- 收听
- 0
该用户从未签到
技术宅认证程序员
苦逼,受剥削人民
- 积分
- 9783
|
发表于 2012-3-12 16:08:36
|
显示全部楼层
这个百度下不就好了- #include <math.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- static float l=3;
- int intersect_test(float,float);//针与平行线相交的次数//
- void main()
- {
-
- start:;
- float y; // sinθ的值//
- float x; //针的中心到最近一条平行线的距离//
- float a=4.0;
- float pi,r;
- int i,N,n=0;
- srand((unsigned)time(0));
- printf(" 实验次数N为: ");
- scanf("%d",&N);
- for(i=0;i<N;i++)
- {
- r= (int(360*rand()/RAND_MAX))/360.0*3.1415;
- if(r>3.1415/2.0) r=r-3.1415/2.0;
- x=(int(360*rand()/RAND_MAX))/360.0*a;
- y=sin(r);
-
- if(intersect_test(y,x) == 1)
-
- n++;
- }
-
-
- pi=float(2*l)/float(a*n/N);
- printf(" 得到pi=%.3f\n",pi);
-
- goto start;
- }
- int intersect_test(float c,float x)
- {
- float y=(l*c)/2;
-
- if(x<=y||y>=(4.0-x))
- return 1;
- else return 0;
- }
复制代码 |
|