Power of 56-2, power of 56-2

Source: Internet
Author: User

Power of 56-2, power of 56-2
Http://lx.lanqiao.cn/problem.page? The time limit for the power expression of gpid = T235 algorithm training 2: 1.0 s memory limit: 512.0 MBProblem description any positive integer can be expressed in binary notation. For example, the binary notation of 137 is 10001001.
This binary representation is written as the sum of the power of the second, so that the power of the second is ranked first. The following expression can be obtained: 137 = 2 ^ 7 + 2 ^ 3 + 2 ^ 0
The power is represented by parentheses, that is, a ^ B is represented as a (B)
In this case, 137 can be expressed as: 2 (7) + 2 (3) + 2 (0)
Further: 7 = 2 ^ 2 + 2 + 2 ^ 0 (2 ^ 1 is expressed as 2)
3 = 2 + 2 ^ 0
So the last 137 can be expressed as: 2 (2 (2) + 2 + 2 (0) + 2 (2 + 2 (0) + 2 (0)
Another example: 1315 = 2 ^ 10 + 2 ^ 8 + 2 ^ 5 + 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) positive integer in the input format (1 <= n <= 20000) the output format conforms to the agreed 0, 2 Representation of n (the representation cannot contain spaces) example input 137 sample output 2 (2 (2) + 2 + 2 (0) + 2 (2 + 2 (0) + 2 (0) sample input 1315 sample output 2 (2 (2 + 2 (0) + 2) + 2 (2 + 2 (0 ))) + 2 (2 (2) + 2 (0) + 2 + 2 (0) prompts that Recursive Implementation is relatively simple and can be output on one side of Recursion

# Include <iostream> # include <cstring> using namespace std; void print (int n) {int a [20000] = {0 }; int len = 0; while (n! = 0) {// obtain the binary a [len ++] = n % 2; n/= 2;} // for (int I = 0; I <len; I ++) // cout <a [I]; int flag = 1; for (int I = len-1; I> = 0; I --) {if (a [I] = 1) {if (flag) {cout <"2"; flag = 0 ;}else {cout <"+ 2 ";} if (I = 0) {cout <"(0)" ;}else if (I = 1) {;} else if (I> 1) {// continue decomposing cout for the power of> 1 <"("; print (I); cout <")" ;}}} int main () {int n; cin> n; print (n); return 0 ;}

  

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.