Advertisement
ibragimova_mariam

Stepik СпортПрогр Генерация Мн-ств длины N из [1, M] элемент

Jul 15th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 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.  
  8. public static void main(String[] args) {
  9. Scanner sc = new Scanner(System.in);
  10.  
  11. int n = sc.nextInt();
  12. int m = sc.nextInt();
  13.  
  14. generate(n, m);
  15. }
  16.  
  17. public static void generate(int n, int m)
  18. {
  19. if(v.size() == n)
  20. {
  21. k++;
  22. if(k == 6659) {
  23. printVector(v);
  24. }
  25. }
  26. else
  27. {
  28. for(int i = 1; i <= m; i++) {
  29. v.add(i);
  30. generate(n, m);
  31. v.remove(v.size() - 1);
  32. }
  33. }
  34. }
  35.  
  36. private static void printVector(List<Integer> v)
  37. {
  38. v.forEach(s -> System.out.print(s + " "));
  39. System.out.println();
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement