连续因子
一个正整数 NN 的因子中可能存在若干连续的数字。例如 630630 可以分解为 3 \times 5 \times 6 \times 73×5×6×7,其中 55、66、77 就是 33 个连续的数字。给定任一正整数 NN,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列。
输入格式
输入在一行中给出一个正整数 NN(1 < N < 2^{31}1<N<2
31
)。
输出格式
首先在第 11 行输出最长连续因子的个数;然后在第 22 行中按因子 1 \times1× 因子 2 \times \cdots \times2×⋯× 因子 kk 的格式输出最小的连续因子序列,其中因子按递增顺序输出,11 不算在内。
输出时每行末尾的多余空格,不影响答案正确性
样例输入
630
样例输出
3
567
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll n;
int ans=0,num=0,sum=0;//分别初始化三个变量
cin>>n;
for(ll i=2;i*i<=n;i++){
if(n%i!=0)continue;//不是因子,跳过
else{
sum=0;//由于是连续的,所以需重置长度
ll temp=n;//记录n的值,防止n发生变化
ll j=i;
while(temp%j==0){//求连续因子
temp/=j;
sum++;
j++;
}//更新数值
if(num<sum) {
ans=i;//最大值
num=sum;//最大长度
}
}
}
if(num==0){
cout<<1<<endl<<n<<endl;
}
else{
cout<<num<<endl;//连续因子的个数
for(int i=0;i<num;i++){
if(i==num-1)cout<<ans+i;
else cout<<ans+i<<"*";
}
}
return 0;
}