import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; public class jed { static String w[] = { "", "1", "1+1", "1+1+1", "(1+1)*(1+1)", "(1+1)*(1+1)+1", "(1+1)*(1+1+1)", "(1+1)*(1+1+1)+1", "(1+1)*(1+1)*(1+1)", "(1+1+1)*(1+1+1)", "(1+1+1)*(1+1+1)+1" }; static int j[] = { 0, 1, 2, 3, 4, 5, 5, 6, 6, 6, 7 }; static int lj; private static int skipWhitespace(final InputStream in) throws IOException { int val = -1; while ((val = in.read()) != -1) { if (!Character.isWhitespace((char) val)) break; } return val; } private static int readInt(final InputStream in) { final StringBuilder b = new StringBuilder(); try { int val = skipWhitespace(in); b.append((char) val); while ((val = in.read()) != -1) { if (Character.isWhitespace((char) val)) break; b.append((char) val); } return Integer.parseInt(b.toString()); } catch (final IOException e) { throw new RuntimeException(e); } } public static String calc(int l) { String res = ""; int z = 10; while (z > 1 && l > 10) { int i = l / z; int r = l % z; if (r < 10) { if (i > 1) { if (i > 10) { res += "(" + w[z] + ")*(" + calc(i) + ")"; lj += j[z]; } else { res += "(" + w[z] + ")*(" + w[i] + ")"; lj += j[z] + j[i]; } } else { res += w[z]; lj += j[z]; } l -= i * z; } z--; } if (l > 0) { res += (res == "" ? "" : "+") + w[l]; lj += j[l]; } return res; } public static void run(final InputStream in, PrintStream out) { int t = readInt(in); for (int i = 0; i < t; i++) { lj = 0; String res = calc(readInt(in)); System.out.println(((res != "" && lj <= 100) ? res : "NIE")); } } /** * @param args */ public static void main(String[] args) { run(System.in, System.out); } }
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 | import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; public class jed { static String w[] = { "", "1", "1+1", "1+1+1", "(1+1)*(1+1)", "(1+1)*(1+1)+1", "(1+1)*(1+1+1)", "(1+1)*(1+1+1)+1", "(1+1)*(1+1)*(1+1)", "(1+1+1)*(1+1+1)", "(1+1+1)*(1+1+1)+1" }; static int j[] = { 0, 1, 2, 3, 4, 5, 5, 6, 6, 6, 7 }; static int lj; private static int skipWhitespace(final InputStream in) throws IOException { int val = -1; while ((val = in.read()) != -1) { if (!Character.isWhitespace((char) val)) break; } return val; } private static int readInt(final InputStream in) { final StringBuilder b = new StringBuilder(); try { int val = skipWhitespace(in); b.append((char) val); while ((val = in.read()) != -1) { if (Character.isWhitespace((char) val)) break; b.append((char) val); } return Integer.parseInt(b.toString()); } catch (final IOException e) { throw new RuntimeException(e); } } public static String calc(int l) { String res = ""; int z = 10; while (z > 1 && l > 10) { int i = l / z; int r = l % z; if (r < 10) { if (i > 1) { if (i > 10) { res += "(" + w[z] + ")*(" + calc(i) + ")"; lj += j[z]; } else { res += "(" + w[z] + ")*(" + w[i] + ")"; lj += j[z] + j[i]; } } else { res += w[z]; lj += j[z]; } l -= i * z; } z--; } if (l > 0) { res += (res == "" ? "" : "+") + w[l]; lj += j[l]; } return res; } public static void run(final InputStream in, PrintStream out) { int t = readInt(in); for (int i = 0; i < t; i++) { lj = 0; String res = calc(readInt(in)); System.out.println(((res != "" && lj <= 100) ? res : "NIE")); } } /** * @param args */ public static void main(String[] args) { run(System.in, System.out); } } |