uva-529-枚举

题意:

a0,a1,a2,a3....an

对于任意的i,j,k

0<=k<=n

0<=i<=k-1

0<=j<=k-1

ak=ai+aj

求a0.....an

解题思路:

迭代加深搜索,限定下界的深搜

首页,利用贪心算法,求出最小的步数depth,an=2ai,当an>=n的时候即可得到最小元素个数,由当前步数一步一步加大搜索的步数.

#include <stdio.h>
#include<iostream>
#include <string.h>
#include<memory.h>
using namespace std;
const int N = ;
int n = ;
int ans[N] = { };
int depth = ;
int flag = ;
void dfs(int cur)
{
if(flag)
return;
if(cur == depth)
{
if(ans[cur] == n)
flag = ;
return;
}
for(int i = ; i <= cur; i++)
{
for(int j = i; j <= cur; j++)
{
if(ans[i] + ans[j] > ans[cur] && ans[i] + ans[j] <= n)
{
int sum = ans[i] + ans[j];
for(int k = cur + ; k <= depth; k++)
sum = sum * ;
if(sum < n)
continue;
ans[cur + ] = ans[i] + ans[j];
dfs(cur + );
if(flag)
return;
}
} }
}
int main()
{
freopen("d://1.txt", "r", stdin);
while (cin >> n && n)
{
memset(ans, , sizeof(ans));
ans[] = ;
int k = ;
depth = ;
flag = ;
while (k < n)
{
++depth;
k = k * ;
}
while (!flag)
{
dfs();
if(!flag)
++depth;
}
cout << ans[];
for(int i = ; i <= depth; i++)
cout << " " << ans[i];
cout << endl;
}
return ;
}
上一篇:clipboardjs复制到粘贴板


下一篇:STL基础复习