回复 10楼 czz5242199
高手能给出每种算法所含的 全部 约数吗?
以下是引用K_Tpanda在2011-11-4 23:23:18的发言:
高手能给出每种算法所含的 全部 约数吗?
高手能给出每种算法所含的 全部 约数吗?
动态规划过程中记录数据,实现起来复杂一点
搜索实现起来很简单,时间复杂度会高一点
程序代码:#include <stdio.h>
#include <stdlib.h>
int n,m,ans,a[100];
void print()
{
int i;
printf("%d=%d",n,a[1]);
for (i=2; i<=m; i++) printf("*%d",a[i]);
printf("\n"); ans++;
}
void dfs(int dep,int pre,int goal)
{
int i;
if (dep==m&&goal>=pre)
{
a[dep]=goal; print(); return;
}
for (i=pre; i<=goal; i++)
if (goal%i==0)
{
a[dep]=i; dfs(dep+1,i,goal/i);
}
}
int main()
{
ans=0;
scanf("%d%d",&n,&m);
int i;
for (i=1; i<=n; i++)
if (n%i==0)
{
a[1]=i; dfs(2,i,n/i);
}
printf("%d\n",ans);
system("pause");
}