#include <iostream>
#include <cmath>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{ int x,y,n,d,j,i,a,c;
d=0;
srand(time(NULL));
cout<<"请输入步数"<<endl;
cin>>n;
x=0;
y=0;
for (j=1;j<=n;j++)
{
j=j*4;
}
for(c=1;c<=j;c++)
{
for (i=1;i<=n;i++)
{
a=(rand()*4)/(RAND_MAX+1)+1;
if(a=1) x=x+1;
if(a=2) x=x-1;
if(a=3) y=y+1;
if(a=4) y=y-1;
}
d=sqrt(x*x+y*y)+d;}
d=d/j;
cout<<"平均距离为"<<d<<endl; 请问大神这个算法哪里出问题了,最后输出的平均值总是0,本算法用于计算上下左右随机走n步,最后离原点的距离的平均值
#include <cmath>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{ int x,y,n,d,j,i,a,c;
d=0;
srand(time(NULL));
cout<<"请输入步数"<<endl;
cin>>n;
x=0;
y=0;
for (j=1;j<=n;j++)
{
j=j*4;
}
for(c=1;c<=j;c++)
{
for (i=1;i<=n;i++)
{
a=(rand()*4)/(RAND_MAX+1)+1;
if(a=1) x=x+1;
if(a=2) x=x-1;
if(a=3) y=y+1;
if(a=4) y=y-1;
}
d=sqrt(x*x+y*y)+d;}
d=d/j;
cout<<"平均距离为"<<d<<endl; 请问大神这个算法哪里出问题了,最后输出的平均值总是0,本算法用于计算上下左右随机走n步,最后离原点的距离的平均值
