题目如下:
AC代码:
#include<iostream>
#include<algorithm>
using namespace std;
string d(int n) {
int a = 0;
string res = "";
while (n > 0) {
if (1 & n) {
if (a == 0)
res = "2(0)";
else if (a == 1) {
if (res != "")
res = "2+" + res;
else
res = "2";
}
else {
string temp = d(a);
if (res != "")
res = "2(" + temp + ")+" + res;
else
res = "2(" + temp + ")";
}
}
a += 1;
n /= 2;
}
return res;
}
int main() {
std::ios::sync_with_stdio(false);
int n;
while (cin >> n) {
string r = d(n);
cout << r << endl;
}
return 0;
}
(个人喜欢在递归函数开始前写递归结束语句,但本题参数n在主函数的意义和在函数内调用的意义有所不同,递归函数中传入是幂指数,递归调用是拆解的数字,故只能在while中用if结束调用,写的很不舒服~~)