OpenJudge 2749 分解因数(递归)

给出一个正整数a,要求分解成若干个正整数的乘积,即a = a1 * a2 * a3 * ... * an,并且1 < a1 <= a2 <= a3 <= ... <= an,问这样的分解的种数有多少。注意到a = a也是一种分解。

Input

第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,包括一个正整数a (1 < a < 32768)

Output

n行,每行输出对应一个输入。输出应是一个正整数,指明满足要求的分解的种数

Sample Input

2
2
20

Sample Output

1
4

这一题博主也是一脸迷茫,想了很长时间愣是没想出来有什么好办法。搜了一些大佬的代码,都是神仙代码,也看不懂。。。

神仙代码如下:

#include <iostream>
#include <cstdio>
using namespace std;
int sum;
void find(int star,int m)
{
    int i;
    for (i=star; i<=m; i++)
    {
        if(m%i==0 && i<=m/i)//当能整除,且小于后一个数
        {
            sum++;
            find(i,m/i);
        }
        if(i>m/i)//前一个数比后一个数大
            break;
    }
}
int main()
{
    int n,t;
    cin>>t;
    while(t--)
    {
        sum=1;//默认a=a的这种情况;
        cin>>n;
        find(2,n);//从2开始
        cout<<sum<<endl;
    }
    return 0;
} 

更神仙的代码:

#include<iostream>
using namespace std;
int n,x;
int f(int a,int b){
    if(a==1) return 1;
    if(b==1) return 0;
    if(a%b==0) return f(a/b,b)+f(a,b-1);
    return f(a,b-1);
}
int main(){
    cin>>n;
    while(n--){
        cin>>x;
        cout<<f(x,x)<<endl;
    }
}

如果有理解的可以给我留言或者私信我讨论讨论。。。非常感谢!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wonder-King

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值