L1-006 连续因子*

1.题意

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

这两句话非常重要也非常难理解,翻译一下:

要求编写程序,求出最长连续因子中的因子个数,并输出最小的连续因子序列

2.代码

#include 
#include 
#include 
#include 
#include 

using namespace std;

typedef long long LL;

LL n;

int main(void)
{
    cin>>n;//输入
    int res = 0;
    int l = 1;//连续因子最小值

    for(int i = 2; i <= sqrt(n) ; i++)
    {
        int temp = n;//暂存
        int count = 0;//计算连续因子个数

        for(int j = i; temp%j == 0&&temp!=0;j++)
        //重中之重!
        {
            temp /= j;
            count ++;
        }
        更新
        if(res

3.理解

for(int j = i; temp%j == 0&&temp!=0;j++)

以N=630为例:

i从2开始遍历,j=2继续,j=3继续,j=4时不满足temp%j == 0,退出遍历,count=2,更新!

i=3,j=3继续,j=4不满足temp%j == 0,退出遍历,count=1

i=4,j=4不满足temp%j == 0,退出遍历,count=0

i=5,j=5继续,j=6继续,j=7继续,j=8不满足temp%j == 0,退出遍历,count=3,更新!

你可能感兴趣的:(PTA,数据结构,算法,c++)