7-6 连续因子 (20分)
大量借鉴了这位大哥的代码
https://blog.csdn.net/weixin_45962741/article/details/112470207
一个正整数 N 的因子中可能存在若干连续的数字。例如 630 可以分解为 3×5×6×7,其中 5、6、7 就是 3 个连续的数字。给定任一正整数 N,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列。
输入格式:
输入在一行中给出一个正整数 N
(1<N<231)。
输出格式:
首先在第 1 行输出最长连续因子的个数;然后在第 2 行中按 因子1*
因子2*……*
因子k 的格式输出最小的连续因子序列,其中因子按递增顺序输出,1 不算在内。
输入样例:
630
输出样例:
3
5*6*7
代码:
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
//初始化
int i,j,n;
int count=0;//计数器
int max=0;//最大序列
int start;
//输入数据
cin>>n;
//
for(i=2;i<=sqrt(n);i++)
{
int t=n;
int p=i;
count=0;
while(t%p==0)
{
count++;
t=t/p;
p++;
}
if(count>max)
{
max=count;
start=i;
}
}
if(max)
{
cout<<max<<endl;
for(i=0;i<max;i++)
{
cout<<start+i;
if(i<max-1)
{
cout<<"*";
}
}
}
else{
cout<<1<<endl<<n;
}
return 0;
}