2018阿里校招算法题--求曲线面积

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <time.h>
/** 请完成下面这个函数,实现题目要求的功能 **/
/** 当然,你也可以不按照这个模板来作答,完全按照自己的想法来 ^-^  **/
/*方法一:利用大量重复试验来解决
*方法二: 求解积分方程(确保收敛)
*借助画图工具:
*/


/*高斯随机序列http://www.cnblogs.com/yeahgis/archive/2012/07/13/2590485.html*/
double gaussrand(double mu, double sigma)
{
    static double V1, V2, S;
    static int phase = 0;
    double X;

    if (phase == 0) {
        do {
            double U1 = (double)rand() / RAND_MAX;
            double U2 = (double)rand() / RAND_MAX;

            V1 = 2 * U1 - 1;
            V2 = 2 * U2 - 1;
            S = V1 * V1 + V2 * V2;
        } while (S >= 1 || S == 0);

        X = V1 * sqrt(-2 * log(S) / S);
    }
    else
        X = V2 * sqrt(-2 * log(S) / S);

    phase = 1 - phase;

    return sigma*X + mu;
}
double leartCurve(double mu1, double sigma1, double mu2, double sigma2) {
    double x, y;
    srand(time(NULL));
    int n = 10000000;
    double bound = 2.0 / sqrt(3.0);
    int count = 0;
    for (int i = 0; i<n; i++) {
        x = gaussrand(mu1, sigma1);
        y = gaussrand(mu2, sigma2);

        if (x >= -bound && x <= 0 && (x*x + y*y + x*y - 1) <= 0)
            count++;
        else if (x <= bound && x >= 0 && (x*x + y*y - x*y - 1) <= 0)
            count++;
    }
    double pro = double(count) / double(n);
    int temp = int(pro * 10);
    return  temp*1.0/10;
}

int main() {
    double res;

    double _mu1;
    scanf("%lf", &_mu1);
    double _sigma1;
    scanf("%lf", &_sigma1);
    double _mu2;
    scanf("%lf", &_mu2);
    double _sigma2;
    scanf("%lf", &_sigma2);

    res = leartCurve(_mu1, _sigma1, _mu2, _sigma2);
    printf("%.1lf\n", res);

    return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值