Advertisement
LeatherDeer

Untitled

Oct 29th, 2022
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. public class RecoveryTask {
  2.     public static void main(String[] args) {
  3.  
  4.         restore("2? + ?5 = 69");
  5.  
  6.     }
  7.  
  8.     private static char[] textGlobal;
  9.     private static List<Integer> operationIndexesGlobal;
  10.  
  11.     public static void restore(String text) {
  12.         ArrayList<Integer> operIndexes = new ArrayList<>();
  13.         char[] textChar = text.replace(" ", "").toCharArray();
  14.         for (int i = 0; i < textChar.length; i++) {
  15.             if (textChar[i] == '+' || textChar[i] == '=') {
  16.                 textChar[i] = '_';
  17.             }
  18.             if (textChar[i] == '?') {
  19.                 operIndexes.add(i);
  20.             }
  21.         }
  22.         textGlobal = textChar;
  23.         operationIndexesGlobal = operIndexes;
  24.  
  25.         CombWithRep(new int[operIndexes.size()], 0, 10);
  26.     }
  27.  
  28.     private static void CombWithRep(int[] comb, int index, int K) {
  29.         if (index == comb.length) {
  30.             checkComb(comb);
  31.             return;
  32.         }
  33.  
  34.         for (int i = 0; i < K; i++) {
  35.             comb[index] = i;
  36.             CombWithRep(comb, index + 1, K);
  37.         }
  38.     }
  39.  
  40.     private static void checkComb(int[] comb) {
  41.         for (int i = 0; i < comb.length; i++) {
  42.             textGlobal[operationIndexesGlobal.get(i)] = Character.forDigit(comb[i], 10);
  43.         }
  44.  
  45.         String[] numbers = String.valueOf(textGlobal).split("_");
  46.  
  47.         int a = Integer.parseInt(numbers[0]);
  48.         int b = Integer.parseInt(numbers[1]);
  49.         int c = Integer.parseInt(numbers[2]);
  50.  
  51.         if (a + b == c) {
  52.             System.out.printf("%d + %d = %d\n", a, b, c);
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement