Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Main{
- public static int n;
- public static int k;
- public static double[] p;
- public static double[] np;
- public static ArrayList<Double> results;
- public static void main(String[] args) {
- In.open("public/test2.in");
- int t = In.readInt();
- for (int i = 0; i < t; i++) {
- n = In.readInt();
- k = In.readInt();
- p = new double[n];
- np = new double [n];
- for (int j = 0; j < n; j++) {
- double pj = In.readDouble();
- if (pj == 1) {
- pj = 0.9999999;
- }
- if (pj == 0) {
- pj = 0.0000001;
- }
- p[j] = pj;
- np[j] = 1 - pj;
- }
- testCase();
- }
- In.close();
- }
- public static void testCase() {
- results = new ArrayList<Double>();
- double finalresult = 1-lessthank();
- finalresult = Math.round(1000000.0 * finalresult) / 1000000.0;
- Out.println(finalresult);
- }
- public static double lessthank(){
- // Berechnung von Arrayeintrag mit 0 Schneetagen
- double prevresult = 1;
- for (int i = 0; i < n; i++){
- prevresult = prevresult * np[i];
- }
- results.add(prevresult);
- // Berechnung von Arraytragen mit 1 bis k-1 Schneetragen
- calculateresults(prevresult, 0, -1);
- // Aufsummieren von Wahrscheinlichkeiten von 0 bis k-1 Schneetagen
- double plessthank = 0;
- for (int i = 0; i < results.size(); i++){
- plessthank = plessthank + results.get(i);
- }
- return plessthank;
- }
- public static void calculateresults(double prevresult, int round, int change){
- if (round == k){
- return;
- }
- if (round != 0){
- prevresult = (prevresult / np[change])* p[change];
- results.add(prevresult);
- }
- change += 1;
- round += 1;
- for (int i = change; i < n; i++){
- calculateresults(prevresult, round, i);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement