1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include<iostream> #include<stack> using namespace std; int n,a[17]; int work(int x) { if(x==0) { cout<<"0"; return 0; } else if(x==2) { cout<<"2"; return 0; } else { while(1) { int i; for(i=16; a[i]>x; i--); if(a[i]!=2) { cout<<"2("; work(i); cout<<")"; } else { cout<<"2"; } if(x-a[i]) { printf("+",x=x-a[i]); } else { return 0; } } } } int main() { a[0]=1; for(int i=1;i<=16;i++) { a[i]=a[i-1]*2; } cin>>n; work(n); return 0; }
|