1501102328-Blue Bridge Cup-recurses representation of algorithm training 2

Source: Internet
Author: User

Algorithm Training 2 recurses represents a time limit: 1.0s memory limit: 512.0MBThe 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 Thinking of solving problemsIt is implemented by recursive return. Convert the decimal number to binary, recording the conversion process as 1 is the first several cycles, and then judge. The boundary of recursion is when n==0,n==1n==2. But it should be noted that when the output + number, when not output. When it's not the last one, output +Define the array as a local variable, because each number of groups is stored differently. Code
#include <stdio.h>int main () {int n;void cimi (int n), while (scanf ("%d", &n)!=eof) {Cimi (n);p rintf ("\ n");} return 0;} void Cimi (int n) {int num;int i,j,k;int a[32];//array defined as local variable num=0;i=0;while (n) {j=n%2;if (j==1)    a[num++]=i;//storage The first few times is 1 I ++;n/=2;} for (i=num-1;i>=0;i--) {if (a[i]==0) printf ("2 (0)"), else if (a[i]==1) printf ("2"), and else if (a[i]==2) printf ("2 (2)"); else if (a[i]>2) {printf ("2 ("); Cimi (A[i]);p rintf (")");} if (i!=0)    printf ("+");//If not the last one, you have to output +  }}


1501102328-Blue Bridge Cup-recurses representation 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.