Recurses Representation of algorithm training 2 (Blue Bridge Cup C + + notation)

Source: Internet
Author: User
Tags pow

The problem describes that any positive integer can be represented by a 2 binary, for example: 137 of the 2 binary is represented as 10001001.
The 2 notation is written in the form of the sum of the power of 2, and the second power is preceded by the following expression: 137=2^7+2^3+2^0
The Covenant power is now represented by parentheses, which means that a^b is represented as a (b)
At this point, 137 can be represented as: 2 (7) +2 (3) +2 (0)
Further: 7=2^2+2+2^0 (2^1 with 2)
3=2+2^0
So the last 137 can be represented as: 2 (2 (2) +2+2 (0)) +2 (+ 0) +2 (0)
Another example: 1315=2^10+2^8+2^5+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) The input format positive integer (1<=n<=20000) output format conforms to the contract N of 0, 2 = (no spaces in the representation) sample input 137 sample Output 2 (2 (2) +2+2 (0)) +2 (0) +2 Example input 1315 sample output 2 (2 (+2 (0)) +2) 2 (0 (+ +2) 2 (2)) +2+2 (0) Prompt with recursive implementation will be relatively simple, can be output on the side of recursion Answer:Before solving such problems, the first thing to think about is recursive implementation; In this case, we first need to find the similarity of this problem: in the topic of the hint: there is a binary way to represent this number, but we can not solve the problem by the conversion of the binary. So we solve them through tentative thinking. By a comparison of the current value with the N power of 2, a power of n is found that is exactly less than the current value of 2. Which means the problem is recursive. Similarity ofIs: Current value = 2 n Power +f (current value-2 n Power) just below the current value;The recursive ExportRelatively easy to find: Export one: The number of the time: Output 2 (0); exit Two: The number is two when: Output 2; The following is the source code:

#include <iostream>
#include <cmath>
using namespace Std;
void Show (int b) {
Export Point Processing
if (b==1) {
cout<< "2 (0)";
return;//must be returned otherwise it will die loop
}
else if (b==2) {
cout<< "2";
return;
}
Similarity point Processing
for (int i=2;i<=15;i++) {//2 0, 1 power equals 1, 2, so find from 2
if (Pow (2,i) >b) {
if (i-1==1) {
Show (2);
}else{
cout<< "2 (";
Show (I-1);
cout<< ")";
}
if (B-pow (2,i-1) >0) {
cout<< "+";
Show (B-pow (2,i-1));
}
Break
}
}
}
int main () {
int b;
cin>>b;
Show (b);
return 0;
}

Recurses Representation of algorithm training 2 (Blue Bridge Cup C + + notation)

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.