import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
/**
* Created by wardziniak on 11/22/16.
*/
public class jed {
static Map<Long, String> basicValues = new HashMap<>();
static Map<Long, Integer> numberOfOnceForBasicValues = new HashMap<>();
static {
basicValues.put(1L, "1");
basicValues.put(2L, "(1+1)");
basicValues.put(3L, "(1+1+1)");
basicValues.put(4L, "(1+1+1+1)");
numberOfOnceForBasicValues.put(1L, 1);
numberOfOnceForBasicValues.put(2L, 2);
numberOfOnceForBasicValues.put(3L, 3);
numberOfOnceForBasicValues.put(4L, 4);
}
public static void main(String[] args) {
solution();
}
public static void solution() {
final Scanner sc = new Scanner(System.in);
int numberOfTestCases = sc.nextInt();
for (int i = 0; i < numberOfTestCases; i++) {
singleSolution(sc);
}
}
public static void singleSolution(Scanner sc) {
long number = sc.nextLong();
int numberOfOpeningBrackets = 0;
Stack<Long> stack = new Stack<>();
stack.push(number);
StringBuilder buffer = new StringBuilder();
int numberOfOnce = 0;
while(!stack.isEmpty() && numberOfOnce <= 101) {
long top = stack.pop();
if (top <= 4) {
buffer.append(basicValues.get(top));
numberOfOnce += numberOfOnceForBasicValues.get(top);
//System.out.print(basicValues.get(top));
continue;
}
if (top % 2 == 0) {
buffer.append("(1+1)*");
numberOfOnce += 2;
//System.out.print("(1+1)*");
stack.push((top/2));
}
else {
buffer.append("(1+");
numberOfOnce += 1;
//System.out.print("(1+");
numberOfOpeningBrackets++;
stack.push((top-1));
}
}
while (numberOfOpeningBrackets-- > 0) {
//System.out.print(")");
buffer.append(")");
}
if (numberOfOnce > 100)
System.out.println("NIE");
else
System.out.println(buffer);
}
}
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 | import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.Stack; /** * Created by wardziniak on 11/22/16. */ public class jed { static Map<Long, String> basicValues = new HashMap<>(); static Map<Long, Integer> numberOfOnceForBasicValues = new HashMap<>(); static { basicValues.put(1L, "1"); basicValues.put(2L, "(1+1)"); basicValues.put(3L, "(1+1+1)"); basicValues.put(4L, "(1+1+1+1)"); numberOfOnceForBasicValues.put(1L, 1); numberOfOnceForBasicValues.put(2L, 2); numberOfOnceForBasicValues.put(3L, 3); numberOfOnceForBasicValues.put(4L, 4); } public static void main(String[] args) { solution(); } public static void solution() { final Scanner sc = new Scanner(System.in); int numberOfTestCases = sc.nextInt(); for (int i = 0; i < numberOfTestCases; i++) { singleSolution(sc); } } public static void singleSolution(Scanner sc) { long number = sc.nextLong(); int numberOfOpeningBrackets = 0; Stack<Long> stack = new Stack<>(); stack.push(number); StringBuilder buffer = new StringBuilder(); int numberOfOnce = 0; while(!stack.isEmpty() && numberOfOnce <= 101) { long top = stack.pop(); if (top <= 4) { buffer.append(basicValues.get(top)); numberOfOnce += numberOfOnceForBasicValues.get(top); //System.out.print(basicValues.get(top)); continue; } if (top % 2 == 0) { buffer.append("(1+1)*"); numberOfOnce += 2; //System.out.print("(1+1)*"); stack.push((top/2)); } else { buffer.append("(1+"); numberOfOnce += 1; //System.out.print("(1+"); numberOfOpeningBrackets++; stack.push((top-1)); } } while (numberOfOpeningBrackets-- > 0) { //System.out.print(")"); buffer.append(")"); } if (numberOfOnce > 100) System.out.println("NIE"); else System.out.println(buffer); } } |
English