递归练手
2025-09-24 21:39:40
发布于:浙江
8阅读
0回复
0点赞
很不错的一道递归练手题
题目大意:
每个数a都能拆成
…… =
而我们需要把所有数(包括分解后的数)拆解成与组合
那么就可以通过递归
每求出一个不为或的数就往下递归
AC代码
#include<bits/stdc++.h>
using namespace std;
void dfs(int x){
int i=0;
stack<int>stk;
while(x!=0){
if(x%2==1){
stk.push(i);
}
x/=2;
i++;
}
int k=0;
while(!stk.empty()){
int t=stk.top();
//cout<<t<<' ';
stk.pop();
if(k!=0){
cout<<'+';
}
k++;
if(t==0){
cout<<"2(0)";
}
else if(t==1){
cout<<"2";
}
else{
cout<<"2(";
dfs(t);
cout<<')';
}
}
}
int main(){
int n;
cin>>n;
dfs(n);
}
这里空空如也






有帮助,赞一个