Power representation of Xidianoj 1024 2

Source: Internet
Author: User

Title Description

Any positive integer can be represented by a power of 2.
Example: 137=2^7+2^3+2^0
At the same time, it is agreed that the square is denoted by parentheses, that is, a^b can be represented as a (b)
The 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 1315 can be represented as: 2 (2 (+ + (0)) +2) +2 (2 (+ + (0)) +2 (2 (2) +2 (0)) +2+2 (0)

Input

Multiple groups, one positive integer per line (0<n<=20000)

Output

Each group of rows, conforming to the 0,2 representation of n (cannot have spaces in the representation)

--body recursion solves the problem of first turning a number into a binary sub-system to solve the power of 1 power times greater than 2 recursion that power is 2 or 0
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;#defineSIZE 20intSolve (intN) {    intk[ -]; intTotal =0, temp =N;  while(Temp >1) { Total++; K[total]= temp%2; Temp/=2; }    if(temp = =1) { Total++; K[total] =1; }    intLast ; inti;  for(i=1; i<=total;i++){        if(K[i] = =1) { last=i;  Break; }    }     for(i=total;i>=1; i--){        if(K[i] = =0)Continue; intnow = i1; if(now = =1) {printf ("2"); }        Else{printf ("2 ("); if(Now >=3) Solve (now); Elseprintf"%d", now); printf (")"); }        if(I! =Last ) {printf ("+"); }    }}intMain () {intN;  while(SCANF ("%d", &n)! =EOF)    {Solve (n); }    return 0;}

Power representation of Xidianoj 1024 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.