2017年0304 内推阿里实习——编程测试,积分求概率

本文介绍了一种使用蒙特卡罗方法计算随机点(X,Y)落入特定心形函数内部概率的程序实现。其中X和Y分别服从不同的正态分布。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

给出心形函数(x*x + y*y - 1)^2 - x*x*y*y  = 0

然后问一个点(X,Y) X服从正态分布(u_x,sigma_x),Y服从正态分布(u_y, sigma_y)

求点(X,Y) 落在心形函数内部的概率。

 

PS: 公式不会推,强行蒙特卡罗法骗20%。。。

 

%20code

#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <limits>
#include <stdio.h>
using namespace std;

bool is_in_love(double x, double y) {
    //new_idea
    if (x*y > 0) {
        return x*x + y*y - 1 - x*y < 0;
    } else {
        return x*x + y*y - 1 + x*y < 0;
    }
    //return (x*x + y*y -1)*(x*x + y*y - 1) - x*x*y*y < 0;//old_idea
}

/** 请完成下面这个函数,实现题目要求的功能 **/
/** 当然,你也可以不按照这个模板来作答,完全按照自己的想法来 ^-^  **/
double leartCurve(double mu1, double sigma1, double mu2, double sigma2) {
    int MAX_T = 1000000;
    double get_time = 0;
    int cnt = 0;
    default_random_engine e; //引擎
    while (cnt < MAX_T) {
        normal_distribution<double> nx(mu1, sigma1); //均值, 方
        double x = nx(e);
        normal_distribution<double> ny(mu2, sigma2);
        double y = ny(e);
        if (is_in_love(x,y)) {
            get_time++;
        }
    }
    return (get_time/MAX_T);
}

int main() {
    double res;
    
    double _mu1;
    cin >> _mu1;
    cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
    
    double _sigma1;
    cin >> _sigma1;
    cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
    
    double _mu2;
    cin >> _mu2;
    cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
    
    double _sigma2;
    cin >> _sigma2;
    cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
    
    
    res = leartCurve(_mu1, _sigma1, _mu2, _sigma2);
    printf("%.1lf\n", res);
    
    return 0;
    
}

 

转载于:https://www.cnblogs.com/chenhuan001/p/6501005.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值