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(), '\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;
    
}
复制代码

 

posted @   chenhuan001  阅读(4280)  评论(7编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2016-03-04 记一道智力题
2016-03-04 RMQ模板
2016-03-04 KMP模板
2016-03-04 扩展KMP模板
点击右上角即可分享
微信分享提示