For each case, the input file contains a positive integer n (n<=20000).
For each case, you should output the exponential form of n an a single line.Note that,there should not be any additional white spaces in the line.
1315
2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = br.readLine()) != null) {
int n = Integer.parseInt(s);
StringBuilder result = new StringBuilder("");
fun(n, result);
System.out.println(result);
}
}
public static void fun(int n, StringBuilder result) {
String num = Integer.toBinaryString(n);
int index = -1;
for (int i = num.length() - 1; i >= 0; --i) {
if (num.charAt(i) != '0') {
index = i;
break;
}
}
for (int i = 0; i < num.length(); i++) {
if (num.charAt(i) != '0') {
result.append(2);
int x = num.length() - 1 - i;
if (x == 0) {
result.append("(0)");
} else if (x == 1) {
result.append("");
} else if (x == 2) {
result.append("(2)");
} else {
result.append("(");
fun(x, result);
result.append(")");
}
if (i != index)
result.append("+");
}
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println(fun(new Scanner(System.in).nextInt()));
}
static String fun(int i) {
char[] array = Integer.toBinaryString(i).toCharArray();
int count =0;
for (char c : array) {
if (c == '1') count++;
}
StringBuilder builder = new StringBuilder();
for (int j = 0; j < array.length; j++) {
if (array[j] == '1') {
count--;
int index = array.length - j;
if (index == 1) {
builder.append("2(0)");
} else if (index == 2) {
builder.append("2");
} else {
builder.append("2(").append(fun(index-1)).append(")");
}
if (count>0)
builder.append("+");
}
}
return builder.toString();
}
}