1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| import java.awt.image.RescaleOp; import java.util.ArrayList; import java.util.Scanner; import java.util.Stack;
import sun.java2d.pipe.SpanIterator;
class Node{ public int num; public double possible; public Node left; // 成功 public Node Right; public int depth; public int failTime; public Node(int num, double possible, int depth, int faliTime) { this.num = num; this.possible = possible; this.depth = depth; this.failTime = faliTime; } } public class Main { private static int N; private static double P; private static double Q; public static float res = 0; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); N = scanner.nextInt(); P = scanner.nextDouble(); Q = scanner.nextDouble(); Node root = new Node(0, 1, 0, 0); root.left = new Node(root.num+1, P, 1, 0); root.Right = new Node(root.num, 1-P, 1, 1); constructLeft(root.left); constructRight(root.Right); getPossible(root, 1); System.out.println(Main.res); } private static void getPossible(Node root, float single) { if(root == null ) { return; } if(root.num <= N) { single *= root.possible; } if(root.num == N) { single *= root.depth; res += single; return; } getPossible(root.left, single); getPossible(root.Right, single); } private static void constructLeft(Node root) { if(root.num == N) { return; } root.left = new Node(root.num+1, P/(2*root.num), root.depth+1, 0); root.Right = new Node(root.num, 1-(root.left.possible), root.depth+1, root.failTime+1); constructLeft(root.left); constructRight(root.Right); } private static void constructRight(Node root) { if(root.num == N) { return; } if(root.possible+Q*root.failTime >= 1) { root.left = new Node(root.num+1, 1, root.depth+1, 0); constructLeft(root.left); }else { root.left = new Node(root.num+1, root.possible+Q*root.failTime, root.depth+1, 0); root.Right = new Node(root.num, 1-(root.left.possible), root.depth+1, root.failTime+1); constructLeft(root.left); constructRight(root.Right); } } }
|