fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. final int numberOfAttempts = 1000000;
  13. int numberOfOccurences = 0;
  14. for (int i=0;i<numberOfAttempts;i++) {
  15. //initialize voters
  16. List<Boolean> voters = new ArrayList<>();
  17. for (int j = 0; j < 7; j++) {
  18. voters.add(true);
  19. }
  20. for (int j = 0; j < 11; j++) {
  21. voters.add(false);
  22. }
  23. Collections.shuffle(voters);
  24. //There shouldn't be any minus after first five votes
  25. boolean minusVote = false;
  26. for (int k = 0; k<5;k++){
  27. if (!voters.get(k)){
  28. minusVote=true;
  29. break;
  30. }
  31. }
  32. if (!minusVote){
  33. numberOfOccurences++;
  34. }
  35. }
  36. System.out.println("Probability: "+100*(double)numberOfOccurences/numberOfAttempts +"%");
  37. }
  38. }
Success #stdin #stdout 1.94s 380160KB
stdin
Standard input is empty
stdout
Probability: 0.2439%