PAT (Advanced Level) Practice 1096 Consecutive Factors (20 分)

时间:2022-07-26
本文章向大家介绍PAT (Advanced Level) Practice 1096 Consecutive Factors (20 分),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1096 Consecutive Factors (20分)

Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×5×6×7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.

Input Specification:

Each input file contains one test case, which gives the integer N (1<N<2​31​​).

Output Specification:

For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format factor[1]*factor[2]*...*factor[k], where the factors are listed in increasing order, and 1 is NOT included.

Sample Input:

630

Sample Output:

3
5*6*7

这题虽然是20分,但是一点也不水,需要认真想一想,2^31次方大概是12!~13!之间,基于此我们可以暴力,大概就是去找它的连续因子,随时更新答案,要注意素数的情况特判~

#include<bits/stdc++.h>
#define ll long long
#define rg register ll
using namespace std;
int main()
{
    ll n,maxx=0,ans=0;
    cin>>n;
    for(rg i=2;i<=sqrt(n);i++)
    {
        ll tep=1,len=0;
        for(rg j=i;j<=sqrt(n);j++)
        {
            tep*=j;
            if(n%tep)break;
            len++;
        }
        if(maxx<len)
        {
            maxx=len;
            ans=i;
        }
    }
    if(!ans)
    {
        cout<<1<<endl<<n<<endl;
    }
    else
    {
        cout<<maxx<<endl;
        for(rg i=ans;i<ans+maxx;i++)
        {
            i==ans+maxx-1?cout<<i<<endl:cout<<i<<"*";
        }
    }
    
    while(1)getchar();
    return 0;
}