Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Main
  5. {
  6. public static void main(String[] args) {
  7. Scanner console = new Scanner(System.in);
  8. System.out.println("Enter number of flips: ");
  9. int numberOfTosses = console.nextInt();
  10. System.out.println("Enter the number of heads you are looking for (number of tails will automatically be total number of tosses minus number of heads) : ");
  11. int numberOfHeads = console.nextInt();
  12. int numberOfTails = numberOfTosses-numberOfHeads;
  13. List<Play> plays = new ArrayList<>();
  14. for (int i = 0; i<(1<<numberOfTosses); i++) {
  15. int curtails = 0, curheads = 0;
  16. int result = 0;
  17. String rep = "";
  18. rep+="["+(i+1)+",] ";
  19. for (int j = 0; j<numberOfTosses; j++) {
  20. if (((1<<(numberOfTosses-1-j))&i)>0)
  21. {
  22. rep+="t ";
  23. result-=(j+1);
  24. curtails++;
  25. }
  26. else {
  27. rep+="h ";
  28. result+=(j+1);
  29. curheads++;
  30. }
  31. }
  32. if (curheads==numberOfHeads) {
  33. plays.add(new Play(rep, result));
  34. }
  35. }
  36. Collections.sort(plays, (play1, play2)->play2.res-play1.res);
  37. System.out.println("We have "+plays.size()+" matches: ");
  38. for (Play p : plays)
  39. {
  40. System.out.println(p);
  41. }
  42. }
  43. }
  44.  
  45. class Play
  46. {
  47. String rep;
  48. int res;
  49.  
  50. public Play(String rep, int res)
  51. {
  52. this.rep = rep;
  53. this.res = res;
  54. }
  55.  
  56. public String toString()
  57. {
  58. return rep+res;
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement