整数因子分解问题

#include "iostream"
#include "algorithm"
#include "fstream"
using namespace std;

/*
h(n)为n的划分数
h(1) = 1
h(2) = 1
h(n) = h(n/2) + h(n/3) + ... + h(n/n)  前提是能被整除
 */

int h[100];

//动态规划
int solve(int n)
{
    h[1] = 1;
    h[2] = 1;
    for(int i=3; i<=n; i++)
        for(int j=2; j<=i; j++)
            if(i%j == 0) //如果j能被i整除
                h[i] += h[i/j];
    return  h[n];
}

//递归
int total = 0;
int recursive(int n)
{
    if(n==1 || n==2)
        return 1;
    for(int i=3; i<=n; i++)
        if(n%i == 0)
            total += recursive(n/i);
    return total;
}

int main()
{
    int n;
    cout << "输入正整数数:";
    cin >> n;
    int count = solve(n);
    cout << "分解式的种数为(动态规划):";
    cout << count << endl;
    cout << "分解式的种数为(递归):";
    count = recursive(n);
    cout << count << endl;
    return 0;
} 

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值