import java.util.Scanner; /** * Created by Patrycja on 23.11.2016. */ public class jed { public static int divisors(int number, int[] divs) { int d = 2, i = 0; double sq = Math.sqrt(number); while(number > 1 && d < sq) { while((number%d) == 0 && i < 50) { divs[i] = d; number = number/d; i++; } d++; } if(i >= 50 || (i == 49 && number > 1)) return -1; if(number > 1) { divs[i] = number; i++; } return i; } public static String str(int number, int[] divs) { StringBuilder str = new StringBuilder(); int b = divisors(number, divs); int sum = 0; if(b < 0) return ""; if(b >= 0) { for(int i = 0; i < b; i++) { int div = divs[i]; sum += div; if(sum > 100) return ""; if(b > 1) str.append('('); for(int j = 0; j < div; j++) { str.append('1'); if(j < div - 1) str.append('+'); } if(b > 1) str.append(')'); if(i < b - 1) str.append('*'); } } return str.toString(); } public static void main(String[] args) { int[] divs = new int[50]; Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int i = 0; i < t; i++) { int number = in.nextInt(); if(number == 1) System.out.println("1"); else { String str = str(number, divs); if(str.isEmpty()) System.out.println("NIE"); else System.out.println(str); } } in.close(); } }
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 | import java.util.Scanner; /** * Created by Patrycja on 23.11.2016. */ public class jed { public static int divisors(int number, int[] divs) { int d = 2, i = 0; double sq = Math.sqrt(number); while(number > 1 && d < sq) { while((number%d) == 0 && i < 50) { divs[i] = d; number = number/d; i++; } d++; } if(i >= 50 || (i == 49 && number > 1)) return -1; if(number > 1) { divs[i] = number; i++; } return i; } public static String str(int number, int[] divs) { StringBuilder str = new StringBuilder(); int b = divisors(number, divs); int sum = 0; if(b < 0) return ""; if(b >= 0) { for(int i = 0; i < b; i++) { int div = divs[i]; sum += div; if(sum > 100) return ""; if(b > 1) str.append('('); for(int j = 0; j < div; j++) { str.append('1'); if(j < div - 1) str.append('+'); } if(b > 1) str.append(')'); if(i < b - 1) str.append('*'); } } return str.toString(); } public static void main(String[] args) { int[] divs = new int[50]; Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int i = 0; i < t; i++) { int number = in.nextInt(); if(number == 1) System.out.println("1"); else { String str = str(number, divs); if(str.isEmpty()) System.out.println("NIE"); else System.out.println(str); } } in.close(); } } |