L1-006. 连续因子

来源:互联网 发布:cfd软件 编辑:程序博客网 时间:2024/05/19 22:26

L1-006. 连续因子

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

一个正整数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

连续的因子可以联想到阶乘,231)数据范围大概在12!,于是可以枚举最长的连续因子个数为12.

#include<iostream>#include<stdio.h>#include <string>#include <cstring>#include <algorithm>#include <cmath>using namespace std;int main(){    int n;    while(~scanf("%d",&n))    {        int t=sqrt(n);        long long ans=1;        int ff=0;        int len,st;        for(len=12; len>=1; len--)        {            for(st=2; st<=t; st++)            {                ans=1;                for(int i=st; i-st<=len-1; i++)                    ans=ans*i;                if(n%ans==0) //如果len=1时还没有因子,那么为素数                {                    ff=1;                    break;                }            }            if(ff)break;        }        if(ff==0) //素数时没有因子,就直接输出了        {            printf("%d\n%d\n",1,n);            continue;        }        printf("%d\n",len);        printf("%d",st);        for(int i=st+1;i-st<=len-1;i++)        {            printf("*%d",i);        }            puts("");        }    return 0;}













0 0