题意:传送门
题解:对于这个题需要找出符合长度最小的长度,对于
100
100
100这个数而言,
1
+
2
+
4
+
6
+
10
+
16
+
26
+
42
>
100
1+2+4+6+10+16+26+42>100
1+2+4+6+10+16+26+42>100,所以有答案的情况大概在
10
10
10以内就存在了,可以直接使用迭代加深搜索,当到达某个深度搜到了答案时就是最短长度了,当然对于每个搜索内部还可以派出等效冗余,如果
x
[
i
]
+
x
[
j
]
x[i]+x[j]
x[i]+x[j]是相等的,搜索多次也是无用,使用
b
o
o
l
bool
bool数组进行标记,加以判重,避免重复搜索某一个和。
c
o
d
e
:
code:
code:
#include<iostream>
#include<cstdio>
using namespace std;
const int N=1e2+50;
int n,path[N];
bool dfs(int u,int depth)
{
if(u==depth)return path[u-1]==n;
bool st[N]={false};
for(int i=u-1;i>=0;i--){
for(int j=u-1;j>=0;j--){
int t=path[i]+path[j];
if(t<=n&&t>path[u-1]&&!st[t]){
st[t]=true;
path[u]=t;
if(dfs(u+1,depth))return true;
}
}
}
return false;
}
int main()
{
while(cin>>n&&n){
path[0]=1;
int depth=1;
while(!dfs(1,depth))depth++;
for(int i=0;i<depth;i++)cout<<path[i]<<" ";
cout<<endl;
}
return 0;
}