South Bridge--recurses expression of algorithm training 2

Source: Internet
Author: User
Tags bitwise

Topic details

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)

Tips
Recursive implementation will be relatively simple, can be output on the side of recursion
Idea: 1, first convert the input 10 number into 2 binary number

2, the binary array bitwise processing, the original decimal number is represented as the corresponding binary representation (for example:137=2^7+2^3+2^0), note the Red flag content

3, judge whether the index can continue to be expressed as a binary system, is the title of the recursive

Note: 1, binary array from the back forward output is the original decimal binary form, so the processing time to start from the last.

2, ^ conversion to (), to increase the output control (), 2^1 to direct output 2, and for 2^0 and 2^2 respectively output 2 (0) and 2 (2).

3, for the control of the Plus, the last one after processing does not need to output a plus, from a one start behind all 0, the bit after processing does not need to output a plus.

Code:

#include <stdio.h>void solve (int n) {//Recursive exit//when exponent is 0 or 2 o'clock, direct output exponent if (n==0 | | n==2) {printf ("%d", n); return;} When the exponent is 1 o'clock (i.e. 2 (1) without output) if (n==1) {return;}/******* above is recursive exit ********/char Str[20];int i=0,j,len,q;//is converted to 2 binary, the resulting string is the original flashback arrangement While  (n) {str[i]=n%2+ ' 0 '; n/=2; i++;} len = i;/*while () is used to mark the count of 0, which is used to control the output of the + sign */q=0; while (str[q]== ' 0 ') {q++;} /  * for 2 binary string bitwise processing *////////variable J to indicate the current processing is the number of the output */for (i--, j=1;i>=0;i--, j + +) {if (str[i]!= ' 0 ') {/* If the currently processed character has a bit ( The bit is 2 (1), then the parentheses are omitted) */if ((len-j) ==1) {printf ("2");} Else{printf ("2 (");} /* Recursive processing */solve (LEN-J);/* If there is a bit after the currently processed character (the bit is 2 (1), the parentheses are omitted) */if ((len-j) ==1); else{printf (")");} /* The last one is currently processed, or the back of the current bit is all 0 without output +*/if ((len-j) ==0 | | i==q); else{printf ("+");}}} int main () {int n;scanf ("%d", &n),//1 handles if (n==1) {printf ("2 (0)") separately;} Else{solve (n);} return 0;}


South Bridge--recurses expression of algorithm training 2

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.