import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class jed { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int numberOfTests = Integer.parseInt(reader.readLine()); for (int n = 0; n < numberOfTests; ++n) { int given = Integer.parseInt(reader.readLine()); if (given == 1) { System.out.println("1"); continue; } int currentOneBit = (Integer.highestOneBit(given) >>> 1); StringBuilder expressionBuilder = new StringBuilder(); for (int j = currentOneBit; j > 1; j >>>= 1) { expressionBuilder.append("(1+1)*("); } if ((currentOneBit & given) != 0) { expressionBuilder.append("1+1+1"); } else { expressionBuilder.append("1+1"); } while (currentOneBit != 1) { currentOneBit >>>= 1; expressionBuilder.append(")"); if ((currentOneBit & given) != 0) { expressionBuilder.append("+1"); } } String expression = expressionBuilder.toString(); System.out.println(expression); } } }
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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class jed { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int numberOfTests = Integer.parseInt(reader.readLine()); for (int n = 0; n < numberOfTests; ++n) { int given = Integer.parseInt(reader.readLine()); if (given == 1) { System.out.println("1"); continue; } int currentOneBit = (Integer.highestOneBit(given) >>> 1); StringBuilder expressionBuilder = new StringBuilder(); for (int j = currentOneBit; j > 1; j >>>= 1) { expressionBuilder.append("(1+1)*("); } if ((currentOneBit & given) != 0) { expressionBuilder.append("1+1+1"); } else { expressionBuilder.append("1+1"); } while (currentOneBit != 1) { currentOneBit >>>= 1; expressionBuilder.append(")"); if ((currentOneBit & given) != 0) { expressionBuilder.append("+1"); } } String expression = expressionBuilder.toString(); System.out.println(expression); } } } |