1331 James goes up the stairs II

Source: Internet
Author: User
Description

Every time James went home, he had to go up n stairs. Sometimes James goes to level 1 and level 2 at a time. Now, James randomly chooses level 1 or Level 2 stairs every time. Could you tell me the expected number of times when it happens to level n. (If Level 2 stairs are to be mounted at level n-1, the whole process will be voided)

Input

The first line is a positive integer T, indicating that there are T groups of data. Each line of the following T line has a positive integer N, indicating that the n-level STAIR (n <= 30) is required ).

Output

For each n, an integer is output, that is, the expected number of times the n-level stairs are mounted. The number of digits after the decimal point is retained.

Sample input 5
1
2
3
5
10
Sample output
1.00
1.33
2.20
3.48
6.78
Prompt

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.