首页 > 专题系列 > Java解POJ > POJ 2651 So you want to be a 2n-aire? [解题报告] Java
2013
11-11

POJ 2651 So you want to be a 2n-aire? [解题报告] Java

So you want to be a 2n-aire?

问题描述 :

The player starts with a prize of $1, and is asked a sequence of n questions. For each question, he may
  • quit and keep his prize.
  • answer the question. If wrong, he quits with nothing. If correct, the prize is doubled, and he continues with the next question.

After the last question, he quits with his prize. The player wants to maximize his expected prize.

Once each question is asked, the player is able to assess the probability p that he will be able to answer it. For each question, we assume that p is a random variable uniformly distributed over the range t .. 1.

输入:

Input is a number of lines, each with two numbers: an integer 1 <= n <= 30, and a real 0 <= t <= 1. Input is terminated by a line containing 0 0. This line should not be processed.

输出:

For each input n and t, print the player’s expected prize, if he plays the best strategy. Output should be rounded to three fractional digits.

样例输入:

1 0.5
1 0.3
2 0.6
24 0.25
0 0

样例输出:

1.500
1.357
2.560
230.138

解题代码:

//* @author:
import java.util.*;
public class Main {
  static double t=0;
  static double e( double s, int k) {
   if( k == 0 )
	return s;
   else {
     double temp = e( 2*s, k-1 );
     double h = s/temp;
     if( t > h )
	return temp*(1-t*t)/2/(1-t);
     else
	return (s*(h-t)+temp*(1-h*h)/2)/(1-t);
    }
}

 static public void main( String [] str ){
   Scanner sc = new Scanner(System.in);
   while(sc.hasNext()) {
       int n=sc.nextInt();
       t=sc.nextDouble();
       if(n==0) break;
       System.out.printf( "%.3f\n", e( 1, n) );
   }
 }
}

  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  2. bottes vernies blanches

    I appreciate the efforts you men and women place in to share blogs on such sort of matters, it was certainly useful. Keep Posting!