首页 > 其他 > 详细

10.31 补题

时间:2020-11-08 22:26:33      阅读:36      评论:0      收藏:0      [点我收藏+]

6.求最长 的连续因子,思路就是从2找到sqrt(n) 然后取最大的,但是改了半天还是wa了一个点,代码:

技术分享图片
#include<bits/stdc++.h>
using namespace std;
#define LL long long
vector<int>p;
int main()
{
    LL t;
    cin>>t;
    LL v=sqrt(t);
    LL r=t;
    LL ans=0,num=0;
    for(LL i=2;i<=v+1;i++)
    {
         num=0;
        LL cnt=0;
        t=r;
        while(1)
        {
            if(t%(i+cnt)==0)
            {
                t=t/(i+cnt);
                cnt++;
                num++;
            }
            else
            {
                if(num>ans)
                {
                    ans=num;
                    p.clear();
                    for(int j=0;j<ans;j++)
                    {
                        p.push_back(j+i);
                    }
                }
                break;
            }
        }
    }
    cout<<ans<<endl;
    for(int i=0;i<p.size();i++)
    {
        if(i==0)cout<<p[i];
        else cout<<"*"<<p[i];
    }
}
View Code

然后就知道,我是从2到sqrt(n)的,如果n是个质数那就取不到,所以如果答案没有的话,就是输出他本身,代码:

技术分享图片
#include<bits/stdc++.h>
using namespace std;
#define LL long long
vector<int>p;
int main()
{
    LL t;
    cin>>t;
    LL v=sqrt(t);
    LL r=t;
    LL ans=0,num=0;
    for(LL i=2;i<=v+100000;i++)
    {
         num=0;
        LL cnt=0;
        t=r;
        while(1)
        {
            if(t%(i+cnt)==0)
            {
                t=t/(i+cnt);
                cnt++;
                num++;
            }
            else
            {
                if(num>ans)
                {
                    ans=num;
                    p.clear();
                    for(int j=0;j<ans;j++)
                    {
                        p.push_back(j+i);
                    }
                }
                break;
            }
        }
    }
    if(p.size()==0)
    {
        cout<<1<<endl;
        cout<<t<<endl;
    }
    else
    {
            cout<<ans<<endl;
        for(int i=0;i<p.size();i++)
        {
            if(i==0)cout<<p[i];
            else cout<<"*"<<p[i];
        }
    }
    
}
View Code

 

10.31 补题

原文:https://www.cnblogs.com/Kingstar1/p/13945709.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!