L1-006. 连续因子

来源:互联网 发布:proe散热模拟软件 编辑:程序博客网 时间:2024/05/19 21:01

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

输入格式:

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

输出格式:

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

输入样例:
630
输出样例:
35*6*7

#include <iostream>#include <algorithm>#include <string>#include <stdio.h>#include <string.h>#include <math.h>#include <vector>#define ll long longusing namespace std;// 三个参数: n 被除数, i 除数, dis 连乘的数量ll fun(ll n, ll i, ll dis){    if (n % i == 0)        return fun(n / i, i + 1, dis + 1);    return dis;}ll n;int main(){    while(cin >> n)    {        ll maxx = -1, ans = -1;        // 查找最长的连续因子        for(ll i = 2;i <= int(sqrt(n));i ++)        {            if (n % i == 0)                {                    ll leng = fun(n / i, i + 1, 1);                    if (leng > maxx)                    {                        maxx = leng;                        ans = i;                    }                }        }        // 没有找到,这个数是素数,输出它本身        if (maxx == -1)        {            cout << 1 << endl;            cout << n << endl;            continue;        }        // 查找到了,输出此时的长度。再依次输出这个序列        cout << maxx << endl;        for (ll i = 0;i < maxx - 1;i ++)            cout << ans + i << "*";        cout << ans + maxx - 1 << endl;    }    return 0;}




0 0