Demo entry 6667110

the rate of two man's birthday in the same day.

   

Submitted by kalimoc on Nov 30, 2017 at 16:16
Language: Matlab. Code size: 472 Bytes.

n=input('请输入人数:');

num=input('请输入实验次数:');;%实验次数

k=0;
for z=1:num
    m=randi([1,365],1,n);%为n个人生成随机生日,在该年开端的第几天。
    flag=0;
    for i=1:(n-1)
        for j=(i+1):n
            if(m(i)==m(j))
                flag=1;
                
                break;  
            end
        end
        if(flag==1)
            break
        end

    end
    if(flag==1)
        k=k+1;
    end
end

fprintf('%d次实验中有%d次至少有两人的生日相同,故概率为%f\r',num,k,k/num);

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).