Bailian2749 分解因数【递归+枚举】

2749:分解因数
总时间限制: 1000ms 内存限制: 65536kB
描述
给出一个正整数a,要求分解成若干个正整数的乘积,即a = a1 * a2 * a3 * … * an,并且1 < a1 <= a2 <= a3 <= … <= an,问这样的分解的种数有多少。注意到a = a也是一种分解。
输入
第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,包括一个正整数a (1 < a < 32768)
输出
n行,每行输出对应一个输入。输出应是一个正整数,指明满足要求的分解的种数
样例输入
2
2
20
样例输出
1
4

问题链接Bailian2749 分解因数
问题简述:(略)
问题分析:采用递归加枚举的方式解决。
程序说明:程序中采取枚举整数所有因子的办法来实现。
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* Bailian2749 分解因数 */

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int fact(int k, int n)
{
    int end = sqrt(n), sum = 1;
    for(int i = k; i <= end; i++)
        if(n % i == 0) sum += fact(i, n / i);
    return sum;
}

int main()
{
    int n, a;
    scanf("%d", &n);
    while(n--) {
        scanf("%d", &a);
        printf("%d\n", fact(2, a));
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章