L1-006 连续因子 C语言实现

一个正整数 N 的因子中可能存在若干连续的数字。例如 630 可以分解为 3×5×6×7,其中 5、6、7 就是 3 个连续的数字。给定任一正整数 N,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列。
输入格式:

输入在一行中给出一个正整数 N(1<N<231)。
输出格式:

首先在第 1 行输出最长连续因子的个数;然后在第 2 行中按 因子1因子2……*因子k 的格式输出最小的连续因子序列,其中因子按递增顺序输出,1 不算在内。
输入样例:

630
输出样例:

3
567

用C语言简便实现

#include<stdio.h>

int LenthFactorSeries(int num,int start);
int IntSqrt(int num);

main()
{
int num,i,supnum,maxlenth=0,maxstart,lenth;
scanf("%d",&num);

supnum=IntSqrt(num);
for(i=2;i<=supnum;i++){
lenth=LenthFactorSeries(num,i);
if(lenth>maxlenth){
maxlenth=lenth;
maxstart=i;
}
}
if(maxlenth==0){
maxlenth=1;
maxstart=num;
}
printf("%d\n",maxlenth);
for(i=0;i<maxlenth;i++){
printf("%d",maxstart+i);
if(i!=maxlenth-1) printf("*");
}

}

int IntSqrt(int num)
{
int intsqrt=(num+1)/2,newsqrt;

while(1){
newsqrt=(intsqrt+num/intsqrt)/2;
if(newsqrt >= intsqrt) break;
intsqrt=newsqrt;
}

return intsqrt;

}

int LenthFactorSeries(int num,int start)
{
int lenth=0;
while(num%start==0){
lenth++;
num/=start;
start++;
}
return lenth;
}

发布了4 篇原创文章 · 获赞 0 · 访问量 51

猜你喜欢

转载自blog.csdn.net/Nexus55/article/details/103914002
今日推荐