d6r11.cpp
来自「工程算法 这是一个很有用的工程数值算法集锦」· C++ 代码 · 共 46 行
CPP
46 行
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
void main()
{
//program d6r11
//driver for routine irbit2
//calculate distribution of runs of zeros
int nbin,ntries,iseed,i,j,iflg,ipts,n;
double delay[16];
nbin = 15;
ntries = 10000;
iseed = 111;
for (i = 1; i<=nbin; i++)
{
delay[i] = 0.0;
}
ipts = 0;
for (i = 1; i <=ntries; i++)
{
if (irbit2(iseed) == 1)
{
ipts = ipts + 1;
iflg = 0;
for (j = 1; j<=nbin; j++)
{
if ((irbit2(iseed) == 1) && (iflg == 0))
{
iflg = 1;
delay[j] = delay[j] + 1.0;
}
}
}
}
cout<< "Distribution of runs of n zeros"<<endl;
cout<< " n probability expected"<<endl;
cout<<setprecision(6)<<setiosflags(ios::fixed);
for (n = 1; n<=nbin; n++)
{
cout<<setw(5)<<n - 1;
cout<<setw(14)<<delay[n] / double(ipts);
cout<<setw(14)<<(1.0/ pow(2,n))<<endl;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?