Pid277/Integer splitting (recursion)

Source: Internet
Author: User

Description

Any positive integer can be expressed by the power of 2. Example: {40%}

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 Format

Input: positive integer (n ≤ 20000)

Output Format

Output: equal to 0, 2 of N (no space is allowed in the representation)


# Include <iostream> # include <cstdio> # include <cmath> using namespace STD; void solve (int n) {int I, B [50], P = 0; for (I = 0; n> 0; I ++, N> = 1) // the fastest way to convert binary data. B [I] = N & 1; for (I --; I> 1; I --) {If (B [I] & 1) {If (P) printf ("+"); printf ("2 ("); solve (I); printf (")"); P = 1 ;}} if (B [1] & 1) // Special Case 1 {If (p) printf ("+"); printf ("2"); P = 1 ;} if (B [0] & 1) // exception 2 {If (p) printf ("+"); printf ("2 (0 )");}} int main () {int N; while (scanf ("% d", & N )! = EOF) {solve (n); printf ("\ n") ;}return 0 ;}


Pid277/Integer splitting (recursion)

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.