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];
        }
    }

}


在這裏插入圖片描述

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