L1-006. 连续因子

来源:互联网 发布:mac mkv 编辑:程序博客网 时间:2024/06/08 05:44

题目地址

https://www.patest.cn/contests/gplt/L1-006

题目描述

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

输入格式:

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

输出格式:

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

输入样例:

630

输出样例:

35*6*7

ac

  • 数据用long long类型,不用int
  • 素数区别
  • 连续因子
#include <cstdio>  #include <cstdlib>  #include <cstring>    #include <string>#include <vector>#include <map>#include <cmath>#include <iostream>#include <queue>using namespace std;const int N = 1005;typedef long long int LL;bool isPrime(LL n){    if(n == 2)        return true;    for(LL i= 2;i*i<=n;i++)    {        if(n % i == 0)            return false;    }    return true;}int main()   {    //freopen("in.txt", "r", stdin);    LL n;    while(scanf("%lld",&n) != EOF)    {        if(isPrime(n))        {            printf("1\n%lld\n",n);            continue;        }        LL ansCnt = 0;        LL staNum = 0;        LL sta = 2;        LL minSums = n;        while(sta * sta <= n)        {            if(n % sta == 0)            {                LL sums = sta;                int cnt = 1;                for(LL k=1;k<=31;k++)                {                    sums *= (sta+k);                    if(n % sums == 0)                    {                        cnt ++;                    }else{                        if(cnt > ansCnt)                        {                            ansCnt = cnt;                            staNum = sta;                            minSums = sums;                        }                        /*                        else if(cnt == ansCnt)                        {                            if(sums < minSums)                            {                                staNum = sta;                                minSums = sums;                            }                        }                        */                        sta = sta + 1;//cnt;                        break;                    }                }            }else{                sta ++;            }        }        printf("%lld\n%lld",ansCnt,staNum);        for(LL i=staNum+1;i<staNum+ansCnt;i++)        {                printf("*%lld",i);        }        printf("\n");    }    //printf("\n");    return 0;  } 
0 0
原创粉丝点击