革命尚未成功

————同志仍需努力————

shinnyblue

导航

L1-006 连续因子

 

题目详情 - L1-006 连续因子 (pintia.cn)

 

 

//【解题思路】找到连续因子串的最前面那个因子the_first_one、最大的连续因子个数maxn,即可完成打印
//【易错1】素数特判
//【易错2】a1*a2*a3*...*an的乘积值也必须是一个因子,简言之,几个小因子的乘积需要是大因子
// 举个例子,虽然2、3、4、5、6、7都分别是1260的因子,但是1260的连续因子并不是2*3*4*5*6*7,因为2*3*4*5*6*7=5040并非1260的因子
#include<iostream>
#include<cmath>
using namespace std;
int n,maxn=-1*(0x3f3f3f3f),cnt,the_first_one,continuous_multiply;//continuous_multiply代表连乘式,用来处理【易错2】
bool isPrime(int x){ //本函数负责素数判断
    if (x==1 || x==4 || x==6 || x==8 || x==9 || x==10)
    {
        return false;
    }
    if (x==2 || x==3 || x==5 || x==7 || x==11)
    {
        return true;
    }
    for (size_t i = 2; i <=sqrt(x) ; i++)
    {
        if (x%i==0)
        {
            return false;
        }
    }
    return true;
}
void _max(int x){ //本函数负责寻找the_first_one、maxn
    for (int i = 2; i <= sqrt(x); i++)
    {
        if (x%i==0)
        {
            cnt=1;
            continuous_multiply=i;
            for (int j = i+1; x%j==0 && x%(continuous_multiply*j)==0; j++)
            {
                continuous_multiply*=j;
                cnt++;
            }
            if (cnt>maxn)
            {
                the_first_one=i;
                maxn=cnt;
            }
        }
    }
    
}
int main()
{
    cin>>n;
    if (isPrime(n))//为什么要加此判断?--->素数的判断只判断到sqrt(n),而题目要求的因子个数是最少也是1(即素数本身),若无此特判则不会输出素数本身
    {
        cout<<1<<'\n'<<n;
    }
    else{
        _max(n);
        cout<<maxn<<endl;
        for (int i = the_first_one; i <= the_first_one+maxn-1; i++)
        {
            if (i<the_first_one+maxn-1)
            {
                cout<<i<<'*';
            }else{
                cout<<i;
            }
        }
    }
    return 0;
}

 

posted on 2023-02-25 12:04  ShinnyBlue  阅读(27)  评论(0编辑  收藏  举报

Live2D