Recurses of 2 means "recursive algorithm training"

Source: Internet
Author: User

"A more Classic Algorithm topic" topic Link: http://lx.lanqiao.org/problem.page?gpid=T235 problem description Any positive integer can be expressed in 2 notation, 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) input format positive integer (1<=n<=20000) output format conforms to the Convention N's 0,2 representation (cannot have spaces in the representation) sample input 137 Sample Output 2 (2 (2) +2+2 (0)) +2 (+ 0) +2 (0) sample input 1315 sample output 2 (2 (+ 0 (+2)) +2) 2 (+ (+ 0)) +2 (2 (2) +2 (0)) +2+2 (0)
Tips
Recursive implementation will be relatively simple, can be recursive side output analysis: The subject can refer to an algorithm: recursive output a decimal number binary representation of the algorithm. (mainly in the recursive backtracking when the output, rather than the calculation of the output.) In the light of the easy-to-hard principle, it is possible to consider a procedure that represents 137 as: 2 (7) +2 (3) +2 (0). About the output of the plus sign: You can consider whether the current item is the highest bit of the binary sequence (the number passed to the next layer is 0). is the highest bit the current item left does not output "+", otherwise output "+" on the left side of the current item. About converting an exponent to a sequence of 0 and 2: If the exponent is more than 1 at the time of the output, the output is "2", and then the exponent and the 0 pass in the recursive function, and the exponent is displayed Then, after the output, the half brackets. Program One: The implementation of 137 is represented as: 2 (7) +2 (3) +2 (0) of the program.
1#include <stdio.h>2 voidTobinary (intNintx);3 intMain ()4 {5     intN;6Freopen ("Out1.txt","W", stdout);7n=1;8      while(n<=255)9     {Ten         //scanf ("%d", &n); Oneprintf"%d---->", n); ATobinary (N,0); -printf"\ n"); -n++; the     } -      -     return 0; - } + voidTobinary (intNintx) - { +     intT; A     if(n==0) at         return; -     Else -     { -t=n%2; -Tobinary (n/2, x+1); -         if(t) in         { -             if(x==1) to             { +                 if(n/2==0) -printf"2"); the                 Elseprintf"+2"); *             } $             ElsePanax Notoginseng             { -                 if(n/2==0) theprintf"2 (%d)", x); +                 Elseprintf"+2 (%d)", x); A             } the         } +     } -}

Program two: The implementation of the complete program.
1#include <stdio.h>2 voidTobinary (intNintx);3 intMain ()4 {5     intN;6     /*scanf ("%d", &n);7 tobinary (n,0);*/8     9Freopen ("Out2.txt","W", stdout);Tenn=1; One      while(n<=255) A     { -         //scanf ("%d", &n); -printf"%d---->", n); theTobinary (N,0); -printf"\ n"); -n++; -     } +      -     return 0; + } A voidTobinary (intNintx) at { -     intT; -     if(n==0) -         return; -     Else -     { int=n%2; -Tobinary (n/2, x+1); to         if(t) +         { -             if(x==1) the             { *                 if(n/2==0) $printf"2");Panax Notoginseng                 Elseprintf"+2"); -             } the             Else +             { A                 if(n/2==0) the                 { +                     //printf ("2 (%d)", x); -                     if(x==0) printf ("2 (0)"); $                     Else $                     { -printf"2 (", x); -Tobinary (x,0); theprintf")"); -                     }Wuyi                 } the                 Else -                 { Wu                     //printf ("+2 (%d)", x); -                     if(x==0) printf ("+2 (0)"); About                     Else $                     { -printf"+2 (", x); -Tobinary (x,0); -printf")"); A                     } +                 } the             } -         } $     } the}

Recurses of 2 means "recursive algorithm training"

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.