实验

三门问题

代码:

#include<bits/stdc++.h>
using namespace std;
int random(int l,int r)//1车
{return rand()%(r-l+1)+l;}
int main()
{
    // set(jzw.cpp,"-static-libgcc -static-libstdc++");
    srand(time(0));
    rand();
    int T;
    printf("请输入实验次数:");
    scanf("%d",&T);
    printf("请输入是否选择更换
换:1   不换:2");
    puts("");
    int x;
    scanf("%d",&x);
    int g=0;
    int f=0;
    if(x==1)
    {
        
        while(T--)
        {
            int s=random(1,3);
            if(s==1)
            f++,printf("未获得
");
            else 
            g++,printf("获得
");
        }
    }
    else
    {
        while(T--)
        {
            int s = random(1,3);
            if(s==1)
                g++,printf("获得
");
            else f++,printf("未获得
");
        }
    }
    printf("获得次数:%d
未获得次数:%d
胜率为:%.3f",g,f,1.0*g/(g+f));   
    system("pause");
}

测试结果:换的胜率近似2/3


红蓝球问题

代码

#include<bits/stdc++.h>
using namespace std;
int col[]={0,1,1,0,1,0,0};
int fcol[]={0,1,1,1,0,0,0};
int random(int l,int r)
{return rand()%(r-l+1)+l;}
int main()
{
    srand(time(0));
    rand();    
    int T;
    printf("请输入实验次数:");
    scanf("%d",&T);
    int red=0,blue=0;
    while(T--)
    {
        int x=random(1,6);
        if(col[x]==0)
        {T++;continue;}
        if(fcol[x]==1)
        {red++;printf("两球全红!
");}
        else 
        {blue++;printf("不符题意!
");}
    }
    printf("都是红色的次数%d 
概率为%.3f",red,1.0*red/(red+blue));
    system("pause");
}

结果:2/3

庞加莱轮回

#include<bits/stdc++.h>
using namespace std;
int a[10];
int n;
bool check()
{
    for(int i=1;i<=n;i++)
    {
        if(a[i]<a[i-1])
        return 1;
    }
    return 0;
}
int main()
{
    printf("请输入序列长度:");
    scanf("%d",&n);
    printf("请输入该序列:");
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    int tot=0;
    while(check())
    {
        random_shuffle(a+1,a+1+n);
        for(int i=1;i<=n;i++)
        printf("%d ",a[i]);
        puts("");
        tot++;
    }
    printf("共用%d次",tot);
    system("pause");
}
原文地址:https://www.cnblogs.com/Marcelo/p/13945595.html