import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.StringTokenizer; import java.util.stream.Collectors; import java.util.stream.Stream; /** * Created by gmatuszewski on 22.11.2016. */ public class jed { public static void main(String[] args) throws IOException { try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out))) { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); while(n-- > 0){ st = new StringTokenizer(br.readLine()); int k = Integer.parseInt(st.nextToken()); resolve(k, bw); bw.append('\n'); } } } private static void resolve(int k, BufferedWriter bw) throws IOException { int close = 0; if (k <=3) { bw.append(Stream.generate(() -> "1").limit(k).collect(Collectors.joining("+"))); return; } while (k > 1){ if (k <=3){ bw.append('(').append(Stream.generate(() -> "1").limit(k).collect(Collectors.joining("+"))).append(')'); break; } boolean odd = k % 2 == 1; if (odd){ bw.append("(1+(1+1)"); close++; } else { bw.append("(1+1)"); } k = k / 2; if (k > 1) bw.append('*'); } while(close-- > 0) bw.append(')'); } }
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 | import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.StringTokenizer; import java.util.stream.Collectors; import java.util.stream.Stream; /** * Created by gmatuszewski on 22.11.2016. */ public class jed { public static void main(String[] args) throws IOException { try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out))) { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); while(n-- > 0){ st = new StringTokenizer(br.readLine()); int k = Integer.parseInt(st.nextToken()); resolve(k, bw); bw.append('\n'); } } } private static void resolve(int k, BufferedWriter bw) throws IOException { int close = 0; if (k <=3) { bw.append(Stream.generate(() -> "1").limit(k).collect(Collectors.joining("+"))); return; } while (k > 1){ if (k <=3){ bw.append('(').append(Stream.generate(() -> "1").limit(k).collect(Collectors.joining("+"))).append(')'); break; } boolean odd = k % 2 == 1; if (odd){ bw.append("(1+(1+1)"); close++; } else { bw.append("(1+1)"); } k = k / 2; if (k > 1) bw.append('*'); } while(close-- > 0) bw.append(')'); } } |