题目链接:P1010 [NOIP1998 普及组] 幂次方 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
代码如下:
# include <iostream>
# include <cmath>
using namespace std;
void function(int x)
{
for(int i = 15; i >= 0; i--)
{
if(pow(2, i) <= x)
{
if(i == 1) cout<<"2";
else if(i == 0) cout<<"2(0)";
else
{
cout<<"2(";
function(i);
cout<<")";
}
x -= pow(2, i);
if(x != 0) cout<<"+";
}
}
}
int main()
{
int n;
cin>>n;
function(n);
return 0;
}
这个题其实就是有嵌套的思想,另一个思路用dfs应该也是可以写的。
949

被折叠的 条评论
为什么被折叠?



