题目描述
Every positive number can be presented by the exponential form.For example, 137 = 2^7 + 2^3 + 2^0。 Let’s present a^b by the form a(b).Then 137 is presented by 2(7)+2(3)+2(0). Since 7 = 2^2 + 2 + 2^0 and 3 = 2 + 2^0 , 137 is finally presented by 2(2(2)+2 +2(0))+2(2+2(0))+2(0). Given a positive number n,your task is to present n with the exponential form which only contains the digits 0 and 2.

示例
输入
1315
输出
2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
解法
#include<iostream> #include<cmath> using namespace std; void generate(int j){ int jj = j; int n=0; if(jj==1)cout<<"2(0)"; if(jj==2)cout<<"2"; if(jj==3)cout<<"2+2(0)"; if(jj==4)cout<<"2(2)"; if(jj==5)cout<<"2(2)+2(0)"; if(jj==6)cout<<"2(2)+2"; if(jj==7)cout<<"2(2)+2+2(0)"; if(jj>7){ if((jj&(jj-1))==0){ for(int i=0;i<16;i++){ if(pow(2,i)==jj){ cout<<"2("; generate(i); cout<<")"; return; } } } while((jj&(jj-1))!=0){ jj--; n++; } generate(jj); cout<<"+"; generate(n); } } int main(){ int a; scanf("%d",&a); generate(a); cout<<endl; return 0; }