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;
    }
}

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

猜你喜欢

转载自blog.csdn.net/qq_42391248/article/details/81082541