用蒙特卡罗方法验证傻子问题


#include 
< stdlib.h >
#include 
< stdio.h >
#include 
< time.h >

#define  uint8 unsigned char
#define  MAXSEAT 100
uint8 g_seat[MAXSEAT]
= {0} ;
uint8 g_seattake[MAXSEAT]
= {0} ;


int  myrand( int  max)
{
    
    
    
return (int)((double)rand() / ((double)RAND_MAX + 1* max+0.5);
}


int  Initseat()
{
    
    
int i;
    
int j;
    
int n;
    
int temp;
    
    
    
for (i=0;i<MAXSEAT;i++)
    
{
        g_seat[i]
=i;
    }


    
    
for (j=0;j<10;j++)
    
for (i=0;i<MAXSEAT;i++)
    
{
        n
=myrand(MAXSEAT-1);
        
        temp
=g_seat[i];
        g_seat[i]
=g_seat[n];
        g_seat[n]
=temp;
    }


            
}



void  main()
{
    
    
long count;//模拟次数
    long suctimes=0//正确次数
    int i;
    
int n;
    
int j;
    
int k;
    
int temp;
    
int tempn;
    srand( (unsigned)time( NULL ) ); 
//
    
    count
=10000;
    
    
for (i=0;i<count;i++)
    
{
        Initseat();
        
        memset(g_seattake,
0,MAXSEAT);
        n
=myrand(MAXSEAT-1);
        
        g_seattake[n]
=1;
        
if (n==g_seat[0])
        
{
            suctimes
++;
        }

        
else
        
{
            
            
for (j=1;j<MAXSEAT-1;j++)
            
{
                
                
if (g_seattake[g_seat[j]])
                
{
                    
                    temp
=myrand(MAXSEAT-j-1);
                    tempn
=0;
                    
for (k=0;k<MAXSEAT;k++)
                    
{
                        
if (g_seattake[k]==0)
                        
{
                        
                            
if (temp==tempn)
                            
{
                                g_seattake[k]
=1;
                                
break;
                            }

                                tempn
++;
                        }

                        
                    }

                    
                }

                
else
                
{
                    g_seattake[g_seat[j]]
=1;
                }

            }

            
            
if (g_seattake[g_seat[j]]==0)
            
{
                suctimes
++;
            }

        }

        
    }

    
    printf(
"suc=%f,v=%d,n=%d ",100.0*suctimes/count,suctimes,count);
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值