Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Main{
  4.  
  5. public static int n;
  6. public static int k;
  7. public static double[] p;
  8. public static double[] np;
  9. public static ArrayList<Double> results;
  10.  
  11. public static void main(String[] args) {
  12. In.open("public/test2.in");
  13. int t = In.readInt();
  14. for (int i = 0; i < t; i++) {
  15. n = In.readInt();
  16. k = In.readInt();
  17. p = new double[n];
  18. np = new double [n];
  19. for (int j = 0; j < n; j++) {
  20. double pj = In.readDouble();
  21. if (pj == 1) {
  22. pj = 0.9999999;
  23. }
  24. if (pj == 0) {
  25. pj = 0.0000001;
  26. }
  27. p[j] = pj;
  28. np[j] = 1 - pj;
  29. }
  30. testCase();
  31. }
  32. In.close();
  33. }
  34.  
  35.  
  36. public static void testCase() {
  37. results = new ArrayList<Double>();
  38. double finalresult = 1-lessthank();
  39. finalresult = Math.round(1000000.0 * finalresult) / 1000000.0;
  40. Out.println(finalresult);
  41. }
  42.  
  43. public static double lessthank(){
  44.  
  45. // Berechnung von Arrayeintrag mit 0 Schneetagen
  46. double prevresult = 1;
  47. for (int i = 0; i < n; i++){
  48. prevresult = prevresult * np[i];
  49. }
  50. results.add(prevresult);
  51.  
  52.  
  53. // Berechnung von Arraytragen mit 1 bis k-1 Schneetragen
  54. calculateresults(prevresult, 0, -1);
  55.  
  56. // Aufsummieren von Wahrscheinlichkeiten von 0 bis k-1 Schneetagen
  57. double plessthank = 0;
  58. for (int i = 0; i < results.size(); i++){
  59. plessthank = plessthank + results.get(i);
  60. }
  61. return plessthank;
  62. }
  63.  
  64. public static void calculateresults(double prevresult, int round, int change){
  65. if (round == k){
  66. return;
  67. }
  68. if (round != 0){
  69. prevresult = (prevresult / np[change])* p[change];
  70. results.add(prevresult);
  71. }
  72. change += 1;
  73. round += 1;
  74. for (int i = change; i < n; i++){
  75. calculateresults(prevresult, round, i);
  76. }
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement