蓝桥杯每日一题:约数个数(质因数)

题目描述:

输入 n 个整数,依次输出每个数的约数的个数。

输入格式

第一行包含整数 n。

第二行包含 n 个整数 ai。

输出格式

共 n 行,按顺序每行输出一个给定整数的约数的个数。

数据范围

1≤n≤1000,
1≤ai≤10^9

输入样例:
5
1 3 4 6 12
输出样例:
1
2
3
4
6

解题思路:

1.枚举每个数,暴力求取每个数的约数;

2.根据n与其质因数的个数关系求取;

参考代码:

###暴力
```
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int n;

int solve(int x)
{
    int res =0;
    for(int i=1;i*i<=x;i++)
    {
        if(x%i==0)
        {
            res++;
            if(i!=x/i) res++;
        }
    }
    return res;
}

int main()
{
    scanf("%d", &n);
    while (n -- )
    {
        int x;
        cin >> x;
        cout<<solve(x)<<endl;
    }
    return 0;
}
```
###质因数分解法
```
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int n;

int solve(int x)
{
    int res = 1;
    for(int i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            int s = 0;
            while(x%i==0) x/=i,s++;
            res *= (s+1);
        }
    }
    
    if(x>1) res *= 2;
    return res;
}

int main()
{
    scanf("%d", &n);
    while (n -- )
    {
        int x;
        cin >> x;
        cout<<solve(x)<<endl;
    }
    return 0;
}
```

 

相关推荐

  1. 2024每日(分解质因数

    2024-04-06 23:24:03       33 阅读
  2. 每日:岛屿个数

    2024-04-06 23:24:03       43 阅读
  3. 每日(python)

    2024-04-06 23:24:03       62 阅读
  4. 每日(BFS)

    2024-04-06 23:24:03       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-06 23:24:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-06 23:24:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-06 23:24:03       87 阅读
  4. Python语言-面向对象

    2024-04-06 23:24:03       96 阅读

热门阅读

  1. 全错排列c++代码

    2024-04-06 23:24:03       35 阅读
  2. 2024.3.23力扣每日一题——统计桌面上的不同数字

    2024-04-06 23:24:03       38 阅读
  3. 《深度学习的数学基础》小结

    2024-04-06 23:24:03       42 阅读
  4. 信息化、数字化、智能化、数智化概念剖析

    2024-04-06 23:24:03       39 阅读
  5. 【C++从0到1-黑马程序员】STL常用算法

    2024-04-06 23:24:03       42 阅读
  6. 一些常见的nginx问题和答案

    2024-04-06 23:24:03       43 阅读
  7. 使用宝塔面板安装nginxVOD点播系统

    2024-04-06 23:24:03       42 阅读
  8. Linux初学(十六)NFS文件共享

    2024-04-06 23:24:03       44 阅读
  9. 15、高精度除法(含源码)

    2024-04-06 23:24:03       36 阅读
  10. numpy库read_excek,读取函数

    2024-04-06 23:24:03       161 阅读