飞机座位分配概率
题目
代码
DP
class Solution {
public:
double nthPersonGetsNthSeat(int n
) {
vector
<double> dp(n
+1);
dp
[1]=1.0;
double sum
=0.0;
for(int i
=2;i
<=n
;i
++)
{
sum
+=dp
[i
-1];
dp
[i
]=sum
/(double)i
;
}
return dp
[n
];
}
};
数学
class Solution {
public:
double nthPersonGetsNthSeat(int n
) {
if(n
==1) return 1.0;
else return 0.5;
}
};
转载请注明原文地址: https://mac.8miu.com/read-497839.html