PTA L1-006 连续因子【暴力模拟】

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

一道暴力题,因为n最大是12的阶层,所以我们可以之间暴力sqrt(n)以内的所有12个连续的数的积,能与n整除就找到了。

这题算是这道题的简易版。

注意一个坑点:这个数本身可以是个素数。

附ac代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <map>
#include <cmath>
#include <set>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef long long ll;
const int maxn = 1e3+10;
int main()
{
    ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    for(int len=12;len>=1;--len)
    {
        
        for(int i=2;i<=sqrt(n);++i)
        {
            ll sum=1;
            for(int j=0;j<len;++j)
            {
                sum*=(i+j);
            }
            if(n%sum==0)
            {
                cout<<len<<endl<<i;
                for(int j=1;j<len;++j)
                {
                    cout<<"*"<<i+j;
                }
                return 0;
            }
        }
        
    }
    cout<<1<<endl<<n;
    return 0;
}

  

posted @ 2018-03-11 15:52  euzmin  阅读(1245)  评论(0编辑  收藏  举报