试除法求约数算法总结

知识概览

  • 试除法求一个数的约数的时间复杂度是

例题展示

题目链接

活动 - AcWing 系统讲解常用算法与数据结构,给出相应代码模板,并会布置、讲解相应的基础算法题目。https://www.acwing.com/problem/content/871/

题解

用试除法求约数,总的时间复杂度是,也就是400万~500万之间。

代码

cpp 复制代码
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

vector<int> get_divisors(int n)
{
    vector<int> res;
    
    for (int i = 1; i <= n / i; i++)
        if (n % i == 0)
        {
            res.push_back(i);
            if (i != n / i) res.push_back(n / i);
        }
        
    sort(res.begin(), res.end());
    return res;
}

int main()
{
    int n;
    cin >> n;
    
    while (n--)
    {
        int x;
        cin >> x;
        auto res = get_divisors(x);
        for (auto t : res) cout << t << ' ';
        cout << endl;
    }
    return 0;
}

参考资料

  1. AcWing算法基础课
相关推荐
黑岚樱梦30 分钟前
代码随想录打卡day23:435.无重叠区间
算法
Kuo-Teng1 小时前
Leetcode438. 找到字符串中所有字母异位词
java·算法·leetcode
gihigo19982 小时前
MATLAB使用遗传算法解决车间资源分配动态调度问题
算法·matlab
墨染点香2 小时前
LeetCode 刷题【138. 随机链表的复制】
算法·leetcode·链表
却道天凉_好个秋2 小时前
目标检测算法与原理(一):迁移学习
算法·目标检测·迁移学习
兮山与3 小时前
算法24.0
算法
晓北斗NorSnow3 小时前
机器学习核心算法与学习资源解析
学习·算法·机器学习
hans汉斯4 小时前
【计算机科学与应用】基于BERT与DeepSeek大模型的智能舆论监控系统设计
大数据·人工智能·深度学习·算法·自然语言处理·bert·去噪
多喝开水少熬夜5 小时前
损失函数系列:focal-Dice-vgg
图像处理·python·算法·大模型·llm