前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT (Advanced Level) Practice 1096 Consecutive Factors (20 分)

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

作者头像
glm233
发布2020-09-28 11:16:49
2240
发布2020-09-28 11:16:49
举报
文章被收录于专栏:glm的全栈学习之路

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:

代码语言:javascript
复制
630

Sample Output:

代码语言:javascript
复制
3
5*6*7

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

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/02/02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Input Specification:
  • Output Specification:
  • Sample Input:
  • Sample Output:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档