zoj 2949 Coins of Luck 抛硬币(概率(期望)+dp)

Luck is the key to life. When I have to decide something, I will make my decision by flipping a coin. And then, I have two things to do. First, I have the decision made. Second, I go to the nearest church and donate the coin.

But there are always too many things I have to decide. For example, what should I eat today for lunch? OK, now we are talking about a decision, so let us assume that I have two kinds of food: quick-served noodle A and quick-served noodle B.

I just have bought N bowls of noodle A and N bowls of noodle B. If I have some space to make a decision (i.e. having two kinds of quick-served noodle to choose from), then I will flip a coin to decide which kind of noodle to eat.

My question is, before I eat up all 2 * N bowls of quick-served noodle, what is the mathematical expectation of the number of coins I will have to donate.

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 1000) which is the number of test cases. And it will be followed by T consecutive test cases.

Each test case contains an integer N (1 <= N <= 1000).

Output

Results should be directed to standard output. The output of each test case should be a single real number, which is the mathematical expectation of the number of coins I have to donate. The result should be accurate to two digits after the fractional point.

Sample Input

2
1
2

Sample Output

1.00
2.50
求抛硬币次数的期望。

double dp[1001][1001];
int main(){	
	memset(dp,0,sizeof(dp));
	for(int i=0;i<=1000;++i){
		dp[0][i]=0;
		dp[i][0]=0;
	}
	for(int i=1;i<=1000;++i){
		for(int j=1;j<=1000;++j){
		dp[i][j]=dp[i][j-1]*0.5+dp[i-1][j]*0.5+1;
	    }
        }
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		printf("%.2lf\n",dp[n][n]);
	}
	return 0;
}


你可能感兴趣的:(zoj 2949 Coins of Luck 抛硬币(概率(期望)+dp))