import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class jed { private static final Map<Integer, String> numbersMap = createMap(); private static final String PLUS = "+"; private static final String TIMES = "*"; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) { int t = sc.nextInt(); String solution = findSolution(t); if (countOnes(solution) > 100) { System.out.println("NIE"); } else { System.out.println(solution); } } } private static int countOnes(String solution) { int count = 0; for (int i = 0; i < solution.length(); i++) { if (solution.charAt(i) == '1') { count++; } } return count; } private static String findSolution(int number) { StringBuilder result = new StringBuilder(); boolean isEmpty = true; boolean lastCharIsDigit = false; // first digit int digit = number % 10; if (digit != 0) { result.append(numbersMap.get(digit)); isEmpty = false; lastCharIsDigit = true; } number -= digit; number /= 10; int power = 1; while (number > 0) { if (lastCharIsDigit) { result.append(PLUS); lastCharIsDigit = false; } digit = number % 10; if (digit == 0) { number /= 10; power++; continue; } if (digit != 1) { result.append(addWithOrWithoutBrackets(digit)); isEmpty = false; result.append(TIMES); } result.append(addTens(power, isEmpty)); isEmpty = false; lastCharIsDigit = true; number -= digit; number /= 10; power++; } return result.toString(); } private static String addWithOrWithoutBrackets(int num) { List<Integer> without = Arrays.asList(1, 6, 8, 9); if (without.contains(num)) { return numbersMap.get(num); } else { return addWithBrackets(num); } } private static String addWithBrackets(int num) { return "(" + numbersMap.get(num) + ")"; } private static String addTens(int power, boolean isEmpty) { StringBuilder tens = new StringBuilder(); if (isEmpty && power == 1) { tens.append(numbersMap.get(10)); } else { tens.append(addWithBrackets(10)); } for (int i = 1; i < power; i++) { tens.append(TIMES); tens.append(addWithBrackets(10)); } return tens.toString(); } private static Map<Integer, String> createMap() { Map<Integer, String> numbersMap = new HashMap<>(10); numbersMap.put(1, "1"); numbersMap.put(2, "1+1"); numbersMap.put(3, "1+1+1"); numbersMap.put(4, "1+1+1+1"); numbersMap.put(5, "1+1+1+1+1"); numbersMap.put(6, "(1+1)*(1+1+1)"); numbersMap.put(7, "(1+1)*(1+1+1)+1"); numbersMap.put(8, "(1+1+1+1)*(1+1)"); numbersMap.put(9, "(1+1+1)*(1+1+1)"); numbersMap.put(10, "(1+1+1)*(1+1+1)+1"); return numbersMap; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class jed { private static final Map<Integer, String> numbersMap = createMap(); private static final String PLUS = "+"; private static final String TIMES = "*"; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) { int t = sc.nextInt(); String solution = findSolution(t); if (countOnes(solution) > 100) { System.out.println("NIE"); } else { System.out.println(solution); } } } private static int countOnes(String solution) { int count = 0; for (int i = 0; i < solution.length(); i++) { if (solution.charAt(i) == '1') { count++; } } return count; } private static String findSolution(int number) { StringBuilder result = new StringBuilder(); boolean isEmpty = true; boolean lastCharIsDigit = false; // first digit int digit = number % 10; if (digit != 0) { result.append(numbersMap.get(digit)); isEmpty = false; lastCharIsDigit = true; } number -= digit; number /= 10; int power = 1; while (number > 0) { if (lastCharIsDigit) { result.append(PLUS); lastCharIsDigit = false; } digit = number % 10; if (digit == 0) { number /= 10; power++; continue; } if (digit != 1) { result.append(addWithOrWithoutBrackets(digit)); isEmpty = false; result.append(TIMES); } result.append(addTens(power, isEmpty)); isEmpty = false; lastCharIsDigit = true; number -= digit; number /= 10; power++; } return result.toString(); } private static String addWithOrWithoutBrackets(int num) { List<Integer> without = Arrays.asList(1, 6, 8, 9); if (without.contains(num)) { return numbersMap.get(num); } else { return addWithBrackets(num); } } private static String addWithBrackets(int num) { return "(" + numbersMap.get(num) + ")"; } private static String addTens(int power, boolean isEmpty) { StringBuilder tens = new StringBuilder(); if (isEmpty && power == 1) { tens.append(numbersMap.get(10)); } else { tens.append(addWithBrackets(10)); } for (int i = 1; i < power; i++) { tens.append(TIMES); tens.append(addWithBrackets(10)); } return tens.toString(); } private static Map<Integer, String> createMap() { Map<Integer, String> numbersMap = new HashMap<>(10); numbersMap.put(1, "1"); numbersMap.put(2, "1+1"); numbersMap.put(3, "1+1+1"); numbersMap.put(4, "1+1+1+1"); numbersMap.put(5, "1+1+1+1+1"); numbersMap.put(6, "(1+1)*(1+1+1)"); numbersMap.put(7, "(1+1)*(1+1+1)+1"); numbersMap.put(8, "(1+1+1+1)*(1+1)"); numbersMap.put(9, "(1+1+1)*(1+1+1)"); numbersMap.put(10, "(1+1+1)*(1+1+1)+1"); return numbersMap; } } |