<>在半径为1.5,一步走1的时候结果是3.528左右,还是比较稳定,大概正负0.003,其他参数的时候没算</P>
<>这段程序可以近似模拟,如有错误欢迎指正<IMG src="http://www.shumo.com/bbs/Skins/Default/emot/em20.gif"></P>
<>#include<iostream><br>#include<cstdlib><br>#include<ctime><br>#include<cmath><br>using namespace std;<br>bool IsOut(const double x,const double y);<br>const double R=1.5;<br>const double step=1;<br>const long MAX=10000000;<br>void main()<br>{<br>double x=0; //初始位置<br>double y=0;<br>double new_x;<br>double new_y;<br>double alpha;<br>double count=0;<br>srand(time(0));<br>for(int i=0;i<MAX;i++)<br>{<br> <br> for(;;)<br> {<br> if(!IsOut(x,y))<br> {<br> alpha=(double(rand())/RAND_MAX)*361;<br> new_x=step*cos(alpha)+x;<br> new_y=step*sin(alpha)+y;<br> x=new_x;<br> y=new_y;<br> count++;<br> }<br> else<br> break;<br> }<br> x=0;<br> y=0;<br>}<br>cout<<"平均需要走"<<count/MAX<<"步"<<endl;<br>}<br>bool IsOut(const double x,const double y)<br>{<br>if((x*x+y*y)<=R*R)<br> return false;<br>else<br> return true;<br>}</P>[em20][em20]
[此贴子已经被作者于2005-10-3 12:29:32编辑过]
|