文章目录
- 加成序列
一、加成序列OJ链接
本题思路:
#include <bits/stdc++.h>
constexpr int N=110;
int n;
int path[N];
bool dfs(int u,int depth)//u表示当前层数,depth表示最大深度
{
if(u==depth) return path[u-1]==n;
bool st[N]={0};
for(int i=u-1;i>=0;i--)
for(int j=i;j>=0;j--){
int s=path[i]+path[j];
if (s > n || s <= path[u - 1] || st[s]) // path一定是递增的
continue;
st[s]=true;
path[u]=s;
if(dfs(u+1,depth)) return true;
}
return false;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);std::cout.tie(nullptr);
path[0]=1;
while(std::cin>>n,n){
int depth=1;
while(!dfs(1,depth)) depth++;
for(int i=0;i<depth;i++) std::cout<<path[i]<<" ";
std::cout<<std::endl;
}
return 0;
}