【跟着英雄学算法第⑩讲】因子分解和枚举——附Leercode刷题题解(C语言实现)

✨前言✨

       在这个系列中,博主准备分享每日在万人千题社区打卡学习的算法。博主也是小白,因此也很能理解新手在刷题时的困惑,所以关注博主,每天学习一道算法吧。同时也欢迎大家加入万人千题习活动,正所谓:一个人可以走的很快,但一群人才能走的更远。

万人千题社区https://bbs.csdn.net/forums/hero?category=0&typeId=17913icon-default.png?t=L9C2https://bbs.csdn.net/forums/hero?category=0&typeId=17913


 一、算法思想笔记

①算法基本定理:

 对于任何整数n,都可以唯一分解成质数的乘积,将相同的质数合并后即可得到

②素因子筛选:

枚举法,埃氏筛,欧拉筛……                                  枚举法+埃氏筛法+欧拉筛法(C语言实现)

③因子分解

 我们在本章节中用枚举法试除每一个可能为素因子的数,实现因子的分解。

小技巧:判断素因子时枚举到 i * i <=n 即可

二、1492.n的第k个因子

n 的第 k 个因子https://leetcode-cn.com/problems/the-kth-factor-of-n/icon-default.png?t=L9C2https://leetcode-cn.com/problems/the-kth-factor-of-n/①题目呈现

 ②代码操练

int kthFactor(int n, int k)
{
    int i;
    int cnt = 0;
    int maxn=sqrt(n+1e-6);//防止浮点数不精确的影响
    int factor[2*maxn];
    factor[0]=0;//factor[0]记录因子的总组数
    for( i = 1; i <= maxn; i++ )
    {
        if(n % i==0 )
        {
            factor[++factor[0]]=i;//别写成==了
        }
    }
    if(maxn*maxn==n)
    {
        cnt=factor[0]*2-1;//cnt计算一共有多少个因子
    }
    else
        cnt = factor[0]*2;
    if( k > cnt )
        return -1;
    if(k <= factor[0] )
        return factor[k];
    else
        return n/factor[1+cnt-k];
}

 思路: 

1.因子分解过程中只需枚举到  i <= maxn 即可确定分解出几组因子

2.只需要再判断maxn*maxn==n,即可确定总组数是奇数个还是偶数个

3.在输出的时候若k>factor[0],只需根据因子的一一对称性即可解

 结果:

 三、1362.最接近的因数

最接近的因数https://leetcode-cn.com/problems/closest-divisors/icon-default.png?t=L9C2https://leetcode-cn.com/problems/closest-divisors/

①题目呈现

 ②代码操练

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int Factor(int n)
{
    int maxn=sqrt(n+1e-6);
    int i;
    for(i = maxn; i>=2;i--)
    {
        if(n % i == 0)
            break;
    }

    return i;
    
}

int* closestDivisors(int num, int* returnSize)
{
    *returnSize=2;
    int n1 = 0;int n2 = 0;
    int*ans = (int *)malloc(sizeof(int)*2);
    n1 = Factor(num+1);
    n2 = Factor(num+2);
    if(( (num+1) / n1 - n1 ) < ( (num+2) / n2 -n2 ))
    {
        ans[0] = n1;
        ans[1] = (num+1) / n1;
    }
    else
    {
        ans[0] = n2;
        ans[1] =(num+2) / n2;
    }
    return ans;
}

思路: 

1.和上一道题类似,不同的是我们从 i = maxn开始递减,这样做的目的是尽快找到最接近的数字

结果:

  • 19
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

罅隙`

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

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

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

打赏作者

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

抵扣说明:

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

余额充值