L1-006. 连续因子

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
输出样例:
3
5*6*7

 分析:1.暴力法,从该数的sqrt(N)开始往前扫,不断更新维护最长连续因子的个数并保存最小的连续因子序列

http://blog.csdn.net/liangzhaoyang1/article/details/51204989

#include "iostream"
#include "cmath"
#include "cstdio"
#include "cstring"
using namespace std;
int main()
{
    int Count,n,N,End,sq;
    char temp[1010],a[1010];///字符串存储 便于拷贝get
    while(~scanf("%d",&N))
    {
        End=1;n=0;
        sq=(int)sqrt(N)+1;
        for(int i=sq;i>=2&&End;i--)
        {
            Count=0;
            if(N%i==0)
            {
                temp[Count++]=i;
                int t=N/i;
                for(int j=i-1;j>=2;j--)
                {
                    if(t%j==0)
                    {
                        temp[Count++]=j;
                        t/=j;
                        if(j==2)
                            End=0;
                    }
                    else
                    {
                        temp[Count]=0;break;
                    }
                }
                if(Count>=n)
                {
                    strcpy(a,temp);
                    n=Count;
                }
            }
        }

        if(n==0)
            printf("%d
%d
",1,N);
        else
        {
            printf("%d
",n);
            for(int i=n-1;i>=0;i--)
            {
                printf("%d",a[i]);
                if(i!=0){
                    printf("*");
                }
                else{
                    printf("
");
                }
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/kimsimple/p/6481718.html