import java.util.Scanner;
import javax.script.ScriptException;
public class jed {
public static void main(String[] args) throws ScriptException {
try (Scanner scanner = new Scanner(System.in)) {
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
ones(scanner.nextInt());
}
}
}
private static void ones(int n) {
if (n == 1) {
System.out.println("1");
return;
}
int closeCount = 0;
boolean mult = false;
while (n > 1) {
if (mult) {
System.out.print("*");
}
if (n % 3 == 0) {
System.out.print("(1+1+1)");
n = n / 3;
mult = true;
} else if (n % 2 == 0) {
System.out.print("(1+1)");
n = n >> 1;
mult = true;
} else {
System.out.print("(1+");
++closeCount;
--n;
mult = false;
}
}
for (int i = 0; i < closeCount; ++i) {
System.out.print(")");
}
System.out.println("");
}
}
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 | import java.util.Scanner; import javax.script.ScriptException; public class jed { public static void main(String[] args) throws ScriptException { try (Scanner scanner = new Scanner(System.in)) { int t = scanner.nextInt(); for (int i = 0; i < t; i++) { ones(scanner.nextInt()); } } } private static void ones(int n) { if (n == 1) { System.out.println("1"); return; } int closeCount = 0; boolean mult = false; while (n > 1) { if (mult) { System.out.print("*"); } if (n % 3 == 0) { System.out.print("(1+1+1)"); n = n / 3; mult = true; } else if (n % 2 == 0) { System.out.print("(1+1)"); n = n >> 1; mult = true; } else { System.out.print("(1+"); ++closeCount; --n; mult = false; } } for (int i = 0; i < closeCount; ++i) { System.out.print(")"); } System.out.println(""); } } |
English