阿里编程题
来源:互联网 发布:dos系统下安装windows 编辑:程序博客网 时间:2024/06/08 11:12
题目:小明向他的女朋友仙仙求婚,在求婚戒指上刻了一个大大的爱心。仙仙看到爱心想考验一下小明,出了一道题。方程(x2+y2−1)2−x2y2=0,这个点在这个爱心里面的概率是多少,精确到0.1(小数点后保留一位小数,比如0.1, 0.2等)
#include <iostream>#include <math.h>using namespace std;/*生成标准正态分布随机数*/double gaussRand(){ 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 X;}/*将随机值从正态分布N(0,1)转化到高斯分布N(mu,sigma)*/double gaussValue(double val,double mu,double sigma){ return mu+sigma*val;}/*判断随机值是否在曲线内*/bool isInside(double x,double y){ double x2=x*x; double y2=y*y; double temp=x2+y2-1; return (temp*temp-x2*y2)>0?false:true;}/*模拟*/double learnCurve(double mu1,double sigma1,double mu2,double sigma2){ int n=10000;//模拟次数 int count=0; int i=n; while(i-->0){ double x=gaussValue(gaussRand(),mu1,sigma1); double y=gaussValue(gaussRand(),mu2,sigma2); if(isInside(x,y)) count++; } return (double) count/n;}int main(){ double mu1,mu2,sigma1,sigma2; cin>>mu1>>sigma1>>mu2>>sigma2; double res=learnCurve(mu1,sigma1,mu2,sigma2); printf("%.1f\n",res); return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
参考:
http://blog.csdn.net/shijing_0214/article/details/60465542
http://www.cnblogs.com/tsingke/p/6194737.html
0 0
- 阿里编程题
- 阿里编程测验题
- 阿里前端编程题1
- 阿里实习生编程题2
- 阿里实习编程题1
- 2017阿里编程测试题
- 阿里秋招编程题
- 阿里2017编程笔试题
- 阿里2017年秋招编程题
- 【编程题】2018阿里编程题题一
- 阿里校园招聘2017编程题
- 阿里内推实习生在线编程题
- 2017阿里实习生在线编程题
- 阿里校招在线编程题
- 2017阿里实习生招聘考试编程题
- 阿里内推编程测试题
- 阿里2017年校招编程题目第二题
- 阿里2018校招笔试编程题
- 友元函数
- 消除^M的方法
- VS2010 有多个重载函数"sqrt"实例与参数列表匹配
- 聊聊RPC之Provider
- nyoj 116士兵杀敌二
- 阿里编程题
- 671B(正常模拟,思维)
- Python+Selenium练习篇之16-点击单选按钮-Radio Button
- 数组,list,arraylist区别
- mysql 远程连接数据库的二种方法
- MyEclipse设置编码方式全部为UTF-8防止乱码
- 最新手机号码,电话号码匹配Java正则表达式
- 自定义jquery插件扩展
- spring笔记