import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class jed { private static final int MAX_FACTOR = 7; private static List<Integer> primeFactors = new ArrayList<>(); private static boolean start = true; public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { int t = scanner.nextInt(); for (int i = 0; i < t; i++) { runOnce(scanner); } } } private static void runOnce(Scanner scanner) { start = true; int number = scanner.nextInt(); if (!checkEasyNumbers(number)) { printResult(number); } System.out.println(); } private static boolean checkEasyNumbers(int number) { if (number == 1) { System.out.print("1"); return true; } if (number == 2) { System.out.print("1+1"); return true; } if (number == 3) { System.out.print("1+1+1"); return true; } if (number == 5) { System.out.print("1+1+1+1+1"); return true; } if (number == 7) { System.out.print("1+1+1+1+1+1+1"); return true; } return false; } private static void printResult(int n) { while (n % 2 == 0) { primeFactors.add(2); n /= 2; } for (int i = 3; i <= MAX_FACTOR; i += 2) { while (n % i == 0) { primeFactors.add(i); n /= i; } } print(primeFactors); primeFactors.clear(); if (n > 1) { if (!start) { System.out.print("*"); } start = false; System.out.print("("); printResult(n - 1); System.out.print("+1)"); } } private static void print(List<Integer> list) { for (int i = 0; i < list.size() - 1; i++) { printNumber(list.get(i)); System.out.print("*"); } if (!list.isEmpty()) { printNumber(list.get(list.size() - 1)); start = false; } } private static void printNumber(int number) { System.out.print("("); for (int i = 1; i < number; i++) { System.out.print("1+"); } System.out.print("1)"); } }
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 | import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class jed { private static final int MAX_FACTOR = 7; private static List<Integer> primeFactors = new ArrayList<>(); private static boolean start = true; public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { int t = scanner.nextInt(); for (int i = 0; i < t; i++) { runOnce(scanner); } } } private static void runOnce(Scanner scanner) { start = true; int number = scanner.nextInt(); if (!checkEasyNumbers(number)) { printResult(number); } System.out.println(); } private static boolean checkEasyNumbers(int number) { if (number == 1) { System.out.print("1"); return true; } if (number == 2) { System.out.print("1+1"); return true; } if (number == 3) { System.out.print("1+1+1"); return true; } if (number == 5) { System.out.print("1+1+1+1+1"); return true; } if (number == 7) { System.out.print("1+1+1+1+1+1+1"); return true; } return false; } private static void printResult(int n) { while (n % 2 == 0) { primeFactors.add(2); n /= 2; } for (int i = 3; i <= MAX_FACTOR; i += 2) { while (n % i == 0) { primeFactors.add(i); n /= i; } } print(primeFactors); primeFactors.clear(); if (n > 1) { if (!start) { System.out.print("*"); } start = false; System.out.print("("); printResult(n - 1); System.out.print("+1)"); } } private static void print(List<Integer> list) { for (int i = 0; i < list.size() - 1; i++) { printNumber(list.get(i)); System.out.print("*"); } if (!list.isEmpty()) { printNumber(list.get(list.size() - 1)); start = false; } } private static void printNumber(int number) { System.out.print("("); for (int i = 1; i < number; i++) { System.out.print("1+"); } System.out.print("1)"); } } |