植物大战僵尸算僵尸死於中間那輛車下的概率

這是最後一隻僵尸,他啃完南瓜后將開始進入大蒜陣。假設:僵尸啃大蒜后將等概率地到相鄰兩行(如果在第1或5行則只能進入2或4行),並且其在某一列啃滿四次后,第五次在該列啃大蒜的時候將進入相鄰行的下一列,到最後一列時會被車推死。問該僵尸死於中間那輛車下的概率

 

#include 
#include 
                                           
int a[5][10]={0},column,row,c,r,i,j=5;
                                           
void movenext()
{
    if (row==0) row=1;
    else
        if (row==4) row=3;
        else
        {
            if (rand()%2==0) row--;
            else
                row++;
        }
                                           
    if (i==4) {column--;i=0;}
    a[row][column]++;i++;
}
                                           
void main()
{
                                               
a[2][9]=1;i=1;row=2;column=9;
while(1)
{
movenext();
if (column==0) break;
}
                                           
                                           
for (r=0;r<5;r++)
{
    for (c=1;c<10;c++)
    {
        printf(" %d",a[r][c]);
    }
    printf("n");
}
                                           
                                           
}

 

#include 
#include 
                                        
int a[5][10]={0},column,row,c,r,i,num=100,j,n;
double gailv[5]={0};
                                        
void movenext()
{
    if (row==0) row=1;
    else
        if (row==4) row=3;
        else
        {
            if (rand()%2==0) row--;
            else
                row++;
        }
                                        
    if (i==4) {column--;i=0;}
    a[row][column]++;i++;
}
                                        
void main()
{
                                            
n=num;
                                        
while(num)
{
for (r=0;r<5;r++)
{
    for (c=1;c<10;c++)
    {
        a[r][c]=0;
    }
}
a[2][9]=1;i=1;row=2;column=9;
while(1)
{
movenext();
if (column==0) {gailv[row]++;break;}
}
num--;
                                        
for (r=0;r<5;r++)
{
    for (c=1;c<10;c++)
    {
        printf(" %d",a[r][c]);
    }
    printf("n");
}
printf("n");
}
                                        
for(j=0;j<5;j++)
{
printf("%fn",gailv[j]/n*1.0);
}
}

 

然后,有人问我要了vb的代码:

Dim a(5, 10) As Integer
Dim column As Integer
Dim row As Integer
Dim c As Integer
Dim r As Integer
Dim i As Integer
Dim num As Integer
Dim j As Integer
Dim n As Integer
Dim gailv(5) As Double
                                 
Function movenext()
Randomize
    If row = 0 Then
    row = 1
    Else
         If row = 4 Then
         row = 3
         Else
            If Int(Rnd * 2) Then
            row = row - 1
            Else
            row = row + 1
            End If
         End If
    End If
If i = 4 Then column = column - 1: i = 0
a(row, column) = a(row, column) + 1
i = i + 1
End Function
                                 
                                 
Private Sub Command1_Click()
num = 100
For r = 0 To 4
For c = 0 To 9
a(r, c) = 0
Next
Next
                                 
For c = 0 To 4
gailv(c) = 0
Next
                                     
n = num
                                 
Do While (num > 0)
                                 
For r = 0 To 4
For c = 0 To 9
a(r, c) = 0
Next
Next
a(2, 9) = 1
i = 1
row = 2
column = 9
Do While (1)
movenext
If column = 0 Then gailv(row) = gailv(row) + 1: Exit Do
Loop
num = num - 1
                                 
'For r = 0 To 4
'For c = 0 To 9
'Print (a(r, c));
'Next
'Print
'Next
                                 
Loop
For j = 0 To 4
Print (gailv(j) / n * 1#)
Next
                                 
                                 
End Sub

 

 

唔~~各位读者,真是不好意思~~

在一位网友的提醒下,我赫然发现由于凭空想错了,我只让僵尸在每列啃了4次~~

只要将第19行的

if (i==4) {column--;i=0;}

改成

if (i==5) {column--;i=0;}

即可~~

故,修改好的完整代码是

#include 
#include 
                      
int a[5][10]={0},column,row,c,r,i,num=9999,j,n;
double gailv[5]={0};
                      
void movenext()
{
    if (row==0) row=1;
    else
        if (row==4) row=3;
        else
        {
            if (rand()%2==0) row--;
            else
                row++;
        }
                      
    if (i==5) {column--;i=0;}
    a[row][column]++;i++;
}
                      
void main()
{
                          
n=num;
                      
while(num)
{
for (r=0;r<5;r++)
{
    for (c=1;c<10;c++)
    {
        a[r][c]=0;
    }
}
a[2][9]=1;i=1;row=2;column=9;
while(1)
{
movenext();
if (column==0) {gailv[row]++;break;}
}
num--;
                      
for (r=0;r<5;r++)
{
    for (c=1;c<10;c++)
    {
        printf(" %d",a[r][c]);
    }
    printf("n");
}
printf("n");
}
                      
for(j=0;j<5;j++)
{
printf("%fn",gailv[j]/n*1.0);
}
}

运行结果是

这跟原来4次的结果完全是反过来的嘛~~~

对了,由于重复100次实在是样本不够大,所以这回我重复了9999次~~~

应该不会再有错了吧~~~不过欢迎大家积极找出我的错误,谢谢啦~~

植物大战僵尸算僵尸死於中間那輛車下的概率》上有1条评论

  1. Pingback引用通告: BZ编程小组 作品 | BZ编程小组

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.