Advertisement
ibragimova_mariam

Stepik СпортПрогр Генерация Перестановок

Jul 15th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class MySolution {
  4.  
  5. private static int k = 0;
  6. private static List<Integer> v = new ArrayList<>();
  7. private static List<Boolean> chosen = new ArrayList<>();
  8.  
  9. public static void main(String[] args) {
  10. Scanner sc = new Scanner(System.in);
  11.  
  12. int n = sc.nextInt();
  13. for(int i = 0; i < n; i++) {
  14. chosen.add(false);
  15. }
  16.  
  17. generate(n);
  18. }
  19.  
  20. public static void generate(int n)
  21. {
  22. if(v.size() == n)
  23. {
  24. k++;
  25. if (k == 4468) {
  26. printVector(v);
  27. }
  28. }
  29. else
  30. {
  31. for(int i = 0; i < n; i++) {
  32. if(chosen.get(i))
  33. continue;
  34.  
  35. chosen.set(i, true);
  36. v.add(i + 1);
  37. generate(n);
  38. chosen.set(i, false);
  39. v.remove(v.size() - 1);
  40. }
  41. }
  42. }
  43.  
  44. private static void printVector(List<Integer> v)
  45. {
  46. v.forEach(s -> System.out.print(s + " "));
  47. System.out.println();
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement