SlavCodes

Untitled

Nov 10th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. package com.telerikacademy.oop;
  2.  
  3.  
  4. import java.util.*;
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9. Scanner reader = new Scanner(System.in);
  10. Random newRandom = new Random();
  11. Set<String> newSet = new TreeSet<>();
  12. int partitionSize = Integer.parseInt(reader.nextLine());
  13. String[] input = reader.nextLine().split(" ");
  14.  
  15. int possibleCombinations = (int)Math.pow(2,partitionSize);
  16.  
  17.  
  18. permutate(newSet,possibleCombinations,input,newRandom,partitionSize);
  19.  
  20. for (String elem:
  21. newSet) {
  22. System.out.println(elem);
  23. }
  24.  
  25.  
  26. }
  27.  
  28. public static void permutate(Set<String> newSet, int possibleCombinations, String[] input, Random rand,int partitionSize){
  29.  
  30. StringBuffer stringBuffer = new StringBuffer();
  31. if(newSet.size() == possibleCombinations){
  32. return;
  33. }
  34.  
  35. for (int i = 0; i < partitionSize; i++) {
  36. stringBuffer.append(input[rand.nextInt(input.length)]);
  37. }
  38.  
  39. newSet.add(String.valueOf(stringBuffer));
  40.  
  41. permutate(newSet,possibleCombinations,input,rand,partitionSize);
  42.  
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment