P1010题解

需要注意的是x==2的特判情况

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) {//(๑ŐдŐ)b
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;//老规矩一个赞一个复制
}//rwr