Consecutive Factors (20)

题目地址:http://www.patest.cn/contests/pat-a-practise/1096

/* PAT1096--浙大2015机试题--Consecutive Factors (20) http://www.patest.cn/contests/pat-a-practise/1096 */
#include <cstdio> 
#include <cstring> 
#include <algorithm> 
#include <vector>
#include <string>
#include <iostream>
#include <cmath>
using namespace std;

long long n; //题目要求 1<N<231)

int main()
{
    freopen("in", "r", stdin);  
    while (scanf("%ld", &n) != EOF)
    {
        int i, j;
        int maxx = -1;
        bool flag = false;
        long long nstart;
        int sqrtn = (int)sqrt(n) + 1;
        for (i = 2; i <= sqrtn; i++)
        {
            long long rs = 1;
            j = i;
            // 主要是这里 循环乘31次
            for (int time = 1; time <= 31; time++)
            {
                rs *= j;
                j++;
                if (rs > n || n%rs != 0)
                    break;
                if (n%rs == 0)
                {
                    if (time > maxx)
                    {
                        maxx = time;
                        flag = true;
                        nstart = i;
                    }
                }
            }
        } // for
        if (!flag) //是prime或其它,直接就是自己打印出来
        {
            printf("1\n%d", n);
        }
        else{
            printf("%d\n", maxx);
            for (i = nstart; i < maxx+nstart; i++)
            {
                if (i == nstart)
                    printf("%d", i);
                else
                    printf("*%d", i);
            }
        }
    }// while
    return 0;
}
/* 630 3 5*6*7 */

你可能感兴趣的:(Consecutive Factors (20))