Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. import java.math.*;
  2. import java.text.*;
  3.  
  4. class Main {
  5.   static double result;
  6.   static double[] prob;
  7.   static double[] flies;
  8.   static int leaves;
  9.    
  10.     public static void main(String[] args) {
  11.         // Uncomment this line if you want to read from a file
  12.         In.open("public/sample.in");
  13.        
  14.         int t = In.readInt();
  15.         for (int i = 0; i < t; i++) {
  16.             testCase();
  17.         }
  18.        
  19.         // Uncomment this line if you want to read from a file
  20.         //In.close();
  21.     }
  22.  
  23.     public static void testCase() {
  24.         // Input using In.java class
  25.         int leaves = In.readInt();
  26.         //Out.print(leaves + " ");
  27.         int startleaf = In.readInt();
  28.         //Out.print(startleaf+ " ");
  29.         int jumps = In.readInt();
  30.         //Out.println(jumps);
  31.        
  32.         flies = new double[leaves+1];
  33.         prob = new double[leaves+1];
  34.         //save values of probs for each leaf :
  35.         for(int i = 0; i<leaves; i++){
  36.           flies[i] = In.readInt();
  37.           //Out.print(flies[i] + " ");
  38.         }
  39.        // Out.println();
  40.         for(int i = 0; i<leaves; i++){
  41.           prob[i] = In.readDouble();
  42.           //Out.print(prob[i] + " ");
  43.         }
  44.         //Out.println();
  45.        
  46.         DecimalFormat df = new DecimalFormat("0.0####");
  47.         df.setRoundingMode(RoundingMode.HALF_DOWN);
  48.         System.out.println(df.format(calcul(startleaf, jumps)));
  49.     }
  50.    
  51.     static double calcul(int startleaf, int jumps){
  52.       if (jumps == 0){
  53.         if(startleaf>=0 && startleaf<=leaves){
  54.         return flies[startleaf];
  55.         }
  56.       }
  57.       double sum = 1.0;
  58.       double left = 1.0;
  59.       double right = 1.0;
  60.       for(int i = 0; i<jumps; i++){//nombre d'additions
  61.         if(startleaf==0){
  62.           if(1-prob[startleaf] == 1.0){//si la prob d'aller à droite ==1
  63.             sum *= (1-prob[startleaf])*calcul(startleaf+1, jumps-1);
  64.             return flies[startleaf] + sum;
  65.           }
  66.           else{
  67.             if(prob[startleaf] == 1.0){
  68.             return flies[startleaf];
  69.           }
  70.           }
  71.         }
  72.         else if(startleaf==leaves){
  73.           if(prob[startleaf] == 1.0){
  74.           sum *= prob[startleaf]*calcul(startleaf-1, jumps-1);
  75.           return flies[startleaf] + sum;
  76.           }
  77.           else if(prob[startleaf] == 0.0){
  78.             return flies[startleaf];
  79.           }
  80.         }
  81.         else{
  82.         left *= prob[startleaf]*calcul(startleaf-1, jumps-1);
  83.         right *= (1-prob[startleaf])*calcul(startleaf+1, jumps-1);
  84.         return flies[startleaf] + left + right;
  85.         }
  86.       }
  87.       return sum;
  88.      
  89.     }
  90.  
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement