Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Main
- {
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- System.out.println("Enter number of flips: ");
- int numberOfTosses = console.nextInt();
- 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) : ");
- int numberOfHeads = console.nextInt();
- int numberOfTails = numberOfTosses-numberOfHeads;
- List<Play> plays = new ArrayList<>();
- for (int i = 0; i<(1<<numberOfTosses); i++) {
- int curtails = 0, curheads = 0;
- int result = 0;
- String rep = "";
- rep+="["+(i+1)+",] ";
- for (int j = 0; j<numberOfTosses; j++) {
- if (((1<<(numberOfTosses-1-j))&i)>0)
- {
- rep+="t ";
- result-=(j+1);
- curtails++;
- }
- else {
- rep+="h ";
- result+=(j+1);
- curheads++;
- }
- }
- if (curheads==numberOfHeads) {
- plays.add(new Play(rep, result));
- }
- }
- Collections.sort(plays, (play1, play2)->play2.res-play1.res);
- System.out.println("We have "+plays.size()+" matches: ");
- for (Play p : plays)
- {
- System.out.println(p);
- }
- }
- }
- class Play
- {
- String rep;
- int res;
- public Play(String rep, int res)
- {
- this.rep = rep;
- this.res = res;
- }
- public String toString()
- {
- return rep+res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement