PAT甲级1096 Consecutive Factors (20分) ****不算简单的题 ,测试点1分析,因式分解

1096 Consecutive Factors (20分)
Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×5×6×7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.

Input Specification:
Each input file contains one test case, which gives the integer N (1<N<2
​31
​​ ).

Output Specification:
For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format factor[1]factor[2]…*factor[k], where the factors are listed in increasing order, and 1 is NOT included.

Sample Input:
630
Sample Output:
3
567

测试点1 可以试试72,如果输出2 3 4 就对了,因为73=233*4

这题最开始看成了n的所有因子中,最长连续的是哪几个,结果还就两个测试点过不了。。。。。。小尴尬

真正的意思是考虑n的所有因式分解情况,最长的连续数字是哪几个。
我还是用的递归,这几天和递归杠上了QAQ,和前天那个 Integer Factorization的题有点相似。
题目让求递归,那就对数字N因式分解的第一因数 第二因数 第三个因数****进行递归,同时为了消除全排列的情况,规定后面因数大于等于前面的因数,比如630,调用递归 df(N,2),表示从2开始寻找因式分解的第一个因数 ,因为630开根号取整是25,所有因式分解的第一个因数i可以从2到25进行尝试,N/i肯定是一个比i大的数。

这里有个小技巧就是 long fatorMax=sqrt(N) 因为我们递归时候有一个因子factor参数,限制了每次递归时候的因子搜素起点,而factorMax把数字N拆成了两部分,小于factorMax的因子 和大于factorMax的因子,我们只需要考虑在factor和factorMax之间的因数就好,这样就能确保我们找到的因式分解都是按照非降的顺序的啦

如果在m次递归时候,没有因数在factor和factorMax之间,本次递归时候的N就是m-1次递归时候 位于factorMax右侧的因数了,这也就确保了非降的顺序,因为在m-1我次递归时候我们确定了一个位于factorMax左侧的因数,N是位于factorMax右侧的因数,肯定是非降的顺序,希望表达清楚了,奈何文字表达实在有限。

		if(N%i==0)
        {
            vec.push_back(i);
            df(N/i,i);
            vec.pop_back();
        }

整个递归函数如下

void df(int N,int factor)
{
    long fatorMax=sqrt(N);
    for(int i=factor; i<=fatorMax; i++)
    {
        if(N%i==0)
        {
            vec.push_back(i);
            df(N/i,i);

            vec.pop_back();
        }
    }
    vec.push_back(N);
    get();
    vec.pop_back();
}

AC总代码如下
其中get函数对递归得到的因式分解进行判断,得出有几个连续数字

#include <bits/stdc++.h>
using namespace std;
vector<int> out;
vector<int> last;
vector<int> vec;
void get()
{

//    if(vec[0]==6&&vec[vec.size()-1]==9)
//        cout<<endl;
//
//    for(int i=0; i<vec.size(); i++)
//        cout<<vec[i]<<' ';
//    cout<<endl;
    
    //vec2主要对于vec的元素去重,否则测试点1中的72=2 3 3 4,只会输出3 4,而不是2 3 4
    vector<int> vec2;
    vec2.push_back(vec[0]);
    for(int i=1; i<vec.size(); i++)
    {
        if(vec[i]==vec[i-1])
            continue;
        else
            vec2.push_back(vec[i]);
    }

    if(vec2.size()<last.size())
        return;
        
    for(int i=0; i<vec2.size(); i++)
    {
        if(out.size()==0)
        {
            out.push_back(vec2[i]);
            continue;
        }
        if(vec2[i]==vec2[i-1]+1)
        {
            out.push_back(vec2[i]);
        }
        else
        {
            if(last.size()<out.size())
            {
                last=out;

            }
            out.clear();
            out.push_back(vec2[i]);
        }
    }
    if(out.size()>last.size())
    {
        last=out;
        out.clear();
    }
}
void df(int N,int factor)
{
    long fatorMax=sqrt(N);
    for(int i=factor; i<=fatorMax; i++)
    {
        if(N%i==0)
        {
            vec.push_back(i);
            df(N/i,i);

            vec.pop_back();
        }
    }
    vec.push_back(N);
    get();
    vec.pop_back();
}
int main(void)
{
    long N;
    cin>>N;
    df(N,2);





    if(last.size()==0)
    {
        cout<<1<<endl;
        cout<<N;
    }
    else
    {
        cout<<last.size()<<endl;
        for(int i=0; i<last.size(); i++)
        {
            if(i==0)
                cout<<last[i];
            else
                cout<<'*'<<last[i];
        }
    }

}


在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章