Problem description
Any positive integer can be represented by a power of 2. For example:
137=27+23+20
At the same time, the square is indicated by parentheses, that is, AB can be expressed as a (b).
The 137 can be expressed as:
2 (7) +2 (3) +2 (0)
Further: 7 = 22+2+20 (21 denoted by 2)
3=2+20
So the last 137 can be expressed as:
2 (2 (2) +2+2 (0)) +2 (+ (0)) +2 (0)
Another example:
1315=210 +28 +25 +2+1
So the last 1315 can be expressed as:
2 (2 (+ + (0)) +2) +2 (2 (+ + (0))) +2 (2 (2) +2 (0)) +2+2 (0)
Input format
The input contains a positive integer N (n<=20000), which is an integer that requires decomposition.
Output format
The program output contains a line of string that is represented by the 0,2 of the conforming n (cannot have spaces in the representation)
Import java.io.*; Public classMain { Public Static void Main(string[] args) throws Exception {BufferedReader BF =NewBufferedReader (NewInputStreamReader (System.inch));intA = Integer.parseint (Bf.readline ());if(a>=1&&a<3) {System. out. println ("2 ("+ (A-1)+")"); }Else{System. out. println (Fun (a)); } } Public StaticString Fun(intNUM) {inttemp = num;Charc []=New Char[]{' 0 ',' 1 '};CharC1 []=New Char[1024x768];intA =1024x768; String s="";if(num>=0&& num<=2){returnnum+""; }Else{ Do{C1[--a] = c [temp&1]; Temp=temp>>1; } while(temp!=0); String S1 =NewString (C1,a,1024x768-a);intI=0; for(i=0; I<s1.length (); i++) {if((S1.charat (i)-' 0 ')==1) {s+="2 ("+fun (S1.length ()-1-I.) +")+"; }} s= S.replaceall ("2\\ (1\\)","2");returnS.substring (0, S.length ()-1); } }}
Welcome to the Csdn-markdown Editor