当前位置:网站首页>【刷题篇】抽牌获胜的概率
【刷题篇】抽牌获胜的概率
2022-06-12 13:02:00 【m0_60631323】
一、题目
谷歌面试题
将面值1-N的牌组成一组
每次从组中等概率的抽出1-N中的一张
下次抽会换一个新的组,有无限组
当抽到的牌累加和<a时,将一直抽牌
当累加和>=a且<b时,你将获胜
当累加和>=b时,你将失败
给定的参数N,a,b 返回获胜的概率
二、题解
核心思想
2.1 递归
public static double f2(int N,int a,int b){
if(N < 1 || a >= b || a < 0){
return 0.0;
}
if(b-a>=N){
return 1.0;
}
return p2(0,N,a,b);
}
//cur 表示目前累加和
public static double p2(int cur,int N,int a,int b){
if(cur>=a&&cur<b){
return 1.0;
}
if(cur>=b){
return 0.0;
}
double w=0.0;
for (int i = 1; i <=N ; i++) {
w+=p2(cur+i,N,a,b);
}
return w/N;
}
2.2 优化递归
优化递归函数中的for循环, 因为计算累加和为cur的获胜概率的时候,需要通过for循环枚举,我们想做的是优化这个循环过程
public static double f3(int N,int a,int b){
if(N < 1 || a >= b || a < 0){
return 0.0;
}
if(b-a>=N){
return 1.0;
}
return p3(0,N,a,b);
}
public static double p3(int cur, int N,int a,int b){
if(cur>=a&&cur<b){
return 1.0;
}
if(cur>=b){
return 0.0;
}
if(cur==a-1){
return 1.0*(b-a)/N;
}
double w=p3(cur+1,N,a,b)+p3(cur+1,N,a,b)*N;
if(cur+1+N<b){
w-=p3(cur+N+1,N,a,b);
}
return w/N;
}
2.3 动态规划
将2.2 的代码改成动态规划即可
public static double f4(int N,int a,int b){
if(N < 1 || a >= b || a < 0){
return 0.0;
}
if(b-a>=N){
return 1.0;
}
double[] dp=new double[b];
for (int i = a; i <b ; i++) {
dp[i]=1.0;
}
if(a-1>=0){
dp[a-1]=1.0*(b-a)/N;
}
for (int cur = a-2; cur >=0 ; cur--) {
double w = dp[cur + 1] + dp[cur + 1] * N;
if (cur + 1 + N < b) {
w -= dp[cur + 1 + N];
}
dp[cur] = w / N;
}
return dp[0];
}
边栏推荐
- Array -- fancy traversal technique of two-dimensional array
- [an Xun cup 2019]iamthinking
- 大一女生废话编程爆火!懂不懂编程的看完都拴Q了
- Index changes of seed points in ITK original image after ROI and downsampling
- Theoretical knowledge of improved DH parameters and standard DH parameters of manipulator
- Install MySQL database independently on Debian 10
- Summary of question brushing in leetcode sliding window
- VGA显示彩条和图片(FPGA)
- Further understanding of the network
- ITK multi-stage registration
猜你喜欢

Openmax (OMX) framework

Volume mount and mirror creation

leetcode 47. Permutations II 全排列 II(中等)

What is the function tag? Article to understand its role and its best practices

torch_geometric message passing network

Image comparison function after registration itk:: checkerboardimagefilter

leetcode 47. Permutations II full permutations II (medium)

Buu question brushing record - 6

403 you don't have permission to access this resource

嵌入式系統硬件構成-基於ARM的嵌入式開發板介紹
随机推荐
【云原生 | Kubernetes篇】Ingress案例实战
Soft test network engineer notes
Index changes of seed points in ITK original image after ROI and downsampling
442个作者100页论文!谷歌耗时2年发布大模型新基准BIG-Bench | 开源
Unittest framework
[an Xun cup 2019]iamthinking
How to adapt the page size when iframe is embedded in a web page
Source of routing information
分享PDF高清版,系列篇
Experience and learning path of introductory deep learning and machine learning
leetcode 47. Permutations II 全排列 II(中等)
VTK image sequence mouse interactive flipping
Eight misunderstandings are broken one by one (2): poor performance? Fewer applications? You worry a lot about the cloud!
Part of the fourth Zhejiang CTF finals
Openmax (OMX) framework
torch_geometric mini batch 的那些事
itk neighbhood
MUI登录数据库完善与AJAX异步处理【MUI+Flask+MongoDB+HBuilderX】
Help you with everything from the basics to the source code. Introduce the technology in detail
Binary tree (thoughts)
