8758: The power of 2 is expressed, and the power of 8758 is expressed

Source: Internet
Author: User

8758: The power of 2 is expressed, and the power of 8758 is expressed
Power of 8758: 2

  • View
  • Submit
  • Statistics
  • Question
Total time limit:
1000 ms
 
Memory limit:
65536kB
Description

Any positive integer can be expressed by the power of 2. For example:

137 = 27 + 23 + 20

At the same time, the conventions are represented by parentheses, that is, AB can be expressed as a (B ). 137 can be expressed:

2 (7) + 2 (3) + 2 (0)

Further: 7 = 22 + 2 + 20 (expressed as 2 in 21)

3 = 2 + 20

So the last 137 can be expressed:

2 (2 (2) + 2 + 2 (0) + 2 (2 + 2 (0) + 2 (0)

Another example:

1315 = 210 + 28 + 25 + 2 + 1

So 1315 can be expressed:

2 (2 (2 + 2 (0) + 2) + 2 (2 (2 + 2 (0) + 2 (2 (2) + 2 (0 )) + 2 + 2 (0)

Input
A positive integer n (n ≤ 20000 ).
Output
A row. It is equal to 0, 2 of n (no space is allowed in the representation ).
Sample Input
137
Sample output
2(2(2)+2+2(0))+2(2+2(0))+2(0)
Source
Question 1 of NOIP1998 semi-finals popularity Group
1 # include <cstdio> 2 # include <cstdlib> 3 # include <cstring> 4 # include <cmath> 5 void work (int n) 6 {7 if (n = 1) 8 {9 printf ("2 (0)"); 10 return; 11} // initial judgment condition, if n is 1 or 2, 12 else if (n = 2) 13 {14 printf ("2"); // The 15return of 2; 16} 17 else 18 {19 int j = 1, I = 0; // j multiplied by 2 each time. If n is greater than, the decomposition ends, 20 // I is the current number (INDEX) 21 do 22 {23 j = 2 * j; 24 if (j> n) 25 {26 j/= 2; // roll back to when j <n at 27 if (I = 1) // this step is very important to determine whether to continue 2 () 28 printf ("2 "); 29 else 30 {3 1 printf ("2 ("); 32 work (I); 33 printf (")"); 34} 35 if (n-j! = 0) 36 {// if there is a remaining number after n decomposition, continue to decompose 37 printf ("+"); 38 work (n-j); 39} 40 return; 41} 42 else43 I ++; 44} while (1); 45} 46} 47 int main () 48 {49 int n; 50 scanf ("% d ", & n); 51 work (n); 52}Click here to view the answer

 

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.