Calculate the probability distribution of the position where the I-th stairway is located.
This is a mathematical problem. The idea is as follows:
First, I set an array P [I] to record the probability of boarding step I. For example, the probability of boarding step 1 is 1/2, the probability of step 2 is 3/4 = 1/2 + 1/2*1/2
The specific formula is: P [I] = 1-P [I-1]/2
Now consider the number of steps I need to climb the expected E [I], apparently can be used in the I-1 step when the level 1 or the I-2 step when the level 2 to reach, the key here is the probability of the two.
Assume that the probability of the former is P1, and the probability of the latter is P2,
Using conditional probability formula (which may be actually Bayesian formula) P1 = P [I-1] * 1/2/P [I]; P2 = P [I-2] * 1/2/P [I];
Then calculate the expected: E [I] = p1 * (E [I-1] + 1) + p2 * (E [I-2] + 1 );
It is better to create a table first.
# Include <cstdio> # include <cstring> # include <iostream> using namespace STD; int main () {int number, T; int op; int I; double P [32]; double PP1, PP2; Double E [32]; P [1] = 0.5; P [2] = 0.75; for (I = 3; I <= 31; I ++) {P [I] = 1-P [I-1]/2;} e [1] = 1.00; E [2] = 4.0/3.0; for (I = 3; I <= 30; I ++) {PP1 = P [I-1] * 0.5/P [I]; PP2 = P [I-2] * 0.5/P [I]; E [I] = PP1 * (E [I-1] + 1) + PP2 * (E [I-2] + 1);} scanf ("% d", & number); For (t = 1; t <= number; t ++) {scanf ("% d", & OP); printf ("%. 2lf \ n ", E [op]);} return 0 ;}