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

题目:

给出心形函数(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(), '
');
    
    double _sigma1;
    cin >> _sigma1;
    cin.ignore (std::numeric_limits<std::streamsize>::max(), '
');
    
    double _mu2;
    cin >> _mu2;
    cin.ignore (std::numeric_limits<std::streamsize>::max(), '
');
    
    double _sigma2;
    cin >> _sigma2;
    cin.ignore (std::numeric_limits<std::streamsize>::max(), '
');
    
    
    res = leartCurve(_mu1, _sigma1, _mu2, _sigma2);
    printf("%.1lf
", res);
    
    return 0;
    
}
原文地址:https://www.cnblogs.com/chenhuan001/p/6501005.html