Home > Back-end >  A power of 2 to the power
A power of 2 to the power

Time:11-11

Their thinking, the code
Any positive integer can be expressed with the power to the power of 2,

Convention use parentheses to represent the party at the same time, namely a, b can be expressed as a (b),

Therefore, 137 can be expressed as:

2 (7) + 2 (3) + 2 (0)

Further:

7=2 (2) + 2 + 2 (0), (2, 1 time in 2)

3=2 + 2 (0)

So 137 can be expressed as:

2 (2 + 2 + 2 (2) (0)) + 2 (2 + 2 (0)) + 2 (0)
According to the

2

Power down time arrangement of time,

Enter
Positive integer n (n
=20000)
O
Expressed in 0, 2, comply with the contract of n (in the table cannot have a space),

The sample input
137
Sample output
2 (2 + 2 + 2 (2) (0)) + 2 (2 + 2 (0)) + 2 (0)
Code to explain
# include
using namespace std;
Void ss (int n, int sum)//n to break down the number, the sum of binary digits,
{
If (n==0)
return;//if n==0, direct return,
Ss (n/2 sum + 1);
If (n % 2)
{
If (n/2)
Cout<& lt;" + ";//if n indicates a 0, says this is not the first two powers, the output plus,
If (sum==1)
Cout<& lt;" 2 ";
The else
{
Cout<& lt;" 2 (the ";
If (sum==0) cout<& lt;" 0 ";
The else ss (sum, 0);
Cout<& lt;" ) ";
}
}
}//if n can not use 2 (0), 2, 2 (2) continues to break down
Int main ()
{
Int a;
Cin> a;
Ss (a, 0);
Coutreturn 0;
}
  • Related