00vk

Decompositions

Jan 15th, 2021 (edited)
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. package stepik.level1.task145;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.Date;
  9. import java.util.List;
  10.  
  11. public class DecompositionsWithArrays {
  12.  
  13.     private static int amount;
  14. //    private static List<String> combinations;
  15.  
  16.     public static void main(String[] args) throws IOException {
  17.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  18.         amount = Integer.parseInt(reader.readLine());
  19.         long before = new Date().getTime();
  20. //        combinations = new ArrayList<>();
  21.  
  22.         int[] combination = new int[amount];
  23.         for (int i = 0; i < amount; i++) {
  24.             combination[i] = 1;
  25.         }
  26.  
  27.         findCombinations(combination);
  28. //        outputResult();
  29.         long after = new Date().getTime();
  30.         System.out.println(after - before + "ms");
  31.     }
  32.  
  33.     private static void findCombinations(int[] combination) {
  34.         while (true) {
  35. //            combinations.add(combinationToString(combination));
  36.             System.out.println(combinationToString(combination));
  37.  
  38.             int lastIndex = combination.length - 1;
  39.             if (lastIndex == 0)
  40.                 return;
  41.  
  42.             int indexToIncrease = lastIndex - 1;
  43.             int leftSum = combination[0] + 1;
  44.             for (int i = indexToIncrease; i > 0; i--) {
  45.                 if (combination[i - 1] > combination[indexToIncrease]) {
  46.                     leftSum += combination[i];
  47.                 } else {
  48.                     indexToIncrease = i - 1;
  49.                 }
  50.             }
  51.  
  52.             int newLength = amount - leftSum + indexToIncrease + 1;
  53.             combination = getNextCombination(combination, indexToIncrease, newLength);
  54.         }
  55.     }
  56.  
  57.     private static int[] getNextCombination(int[] source, int indexToIncrease, int newLength) {
  58.         int[] nextCombination = new int[newLength];
  59.         for (int i = 0; i < newLength; i++) {
  60.             if (i < indexToIncrease) {
  61.                 nextCombination[i] = source[i];
  62.             } else if (i == indexToIncrease) {
  63.                 nextCombination[i] = source[i] + 1;
  64.             } else {
  65.                 nextCombination[i] = 1;
  66.             }
  67.         }
  68.         return nextCombination;
  69.     }
  70.  
  71.     private static String combinationToString(int[] combination) {
  72.         StringBuilder builder = new StringBuilder().append(combination[0]);
  73.         for (int i = 1; i < combination.length; i++) {
  74.             builder.append(" ").append(combination[i]);
  75.         }
  76.         return builder.toString();
  77.     }
  78.  
  79. //    private static void outputResult() {
  80. //        Collections.sort(combinations);
  81. //        for (int i = 0; i < combinations.size(); i++) {
  82. //            System.out.println(combinations.get(i));
  83. //        }
  84. //    }
  85. }
  86.  
Add Comment
Please, Sign In to add comment