Question 1095: 2 power exercise Recursion

Source: Internet
Author: User
Description:

Every positive number can be presented by the exponential form. For example, 137 = 2 ^ 7 + 2 ^ 3 + 2 ^ 0.

Let's present a ^ B by the Form A (B ). then 137 is presented by 2 (7) + 2 (3) + 2 (0 ). since 7 = 2 ^ 2 + 2 + 2 ^ 0 and 3 = 2 + 2 ^ 0,137 is finally presented by 2 (2 (2) + 2 + 2 (0 )) + 2 (2 + 2 (0) + 2 (0 ).
 
Given a positive number N, your task is to present n with the exponential form which only contains the digits 0 and 2.

Input:

For each case, the input file contains a positive integer n (n <= 20000 ).

Output:

For each case, You shocould output the exponential form of n a single line. Note that, There shocould not be any additional white spaces in the line.

Sample input:
1315
Sample output:
 
2 (2 (2 + 2 (0) + 2) + 2 (2 (2 + 2 (0) + 2 (2 (2) + 2 (0 )) + 2 + 2 (0)
 
 
 Package Shanghai Jiao Tong University;

Import Java. util. vendor;

Public Class Power: 1095 {
Static Int N;
Public Static Void Main (string [] ARGs ){
Scanner S = New Using (system. In );
While (S. hasnextint ()){
N = S. nextint ();
System. Out. println (f (n ));
}
}
Static String F ( Int N ){
Stringbuffer sb = New Stringbuffer ();
If (N <8 ){
Switch (N ){
Case 0: Return "0 ";
Case 1: Return "";
Case 2: Return "2 ";
Case 3: Return "2 + 2 (0 )";
Case 4: Return "2 (2 )";
Case 5: Return "2 (2) + 2 (0 )";
Case 6: Return "2 (2) + 2 ";
Case 7: Return "2 (2) + 2 + 2 (0 )";
}
} Else {
String temp = integer. tobinarystring (N );
Int Len = temp. Length ();
For ( Int I = 0; I <Len; I ++ ){
If (Temp. charat (I) = '1 '){
If (I! = 0)
SB. append ("+ ");
SB. append ("2 (");
SB. append (f (len-i-1 ));
SB. append (")");
}
}
}
Return SB. tostring (). replaceall ("\\(\\)","");
}
}
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.