1096. Consecutive Factors (20)——PAT (Advanced Level) Practise

题目信息

1096. Consecutive Factors (20)

时间限制400 ms

内存限制65536 kB

代码长度限制16000 B

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

解题思路

分奇偶两种情况讨论

AC代码

#include <algorithm>
#include<set>
#include<queue>
#include<map>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;

void dfsFactor(long long n, long long preNum, vector<int>&factor, vector<vector<int>>&ans,int&factorSize)
{
    if (n % (preNum + 1) == 0)
    {
        factor.push_back(preNum + 1);
        dfsFactor(n / (preNum + 1), preNum + 1, factor, ans, factorSize);
    }
    else if (!factor.empty() && factor.size() > factorSize)
    {
        factorSize = factor.size();
        ans.push_back(factor);
    }
}
bool cmp(const vector<int>&a, const vector<int>&b)
{
    if (a.size() > b.size())
        return true;
    else if (a.size() == b.size() && a[0] < b[0])
        return true;
    else return false;
}
int main(void)
{
    int n;
    cin >> n;
        if (n % 2 == 1)
        {
            int temp = (int)((double)sqrt(n) + 1);
            for (int i = 2; i <= temp; ++i)
            {
                if (n%i == 0)
                {
                    cout << "1" << endl;
                    cout << i << endl;
                    return 0;
                }
            }
            cout << "1" << endl;
            cout << n << endl;

        }
        else
        {
            vector<int> maxFactor = { INT_MAX, n, (int)((double)sqrt(n) + 1), 1290, 215, 73, 35, 21, 14, 10, 0 };//3~13
            vector<int> factor(0);
            vector<vector<int>>ans(0);
            int factorSize = 0;

            for (long long i = 1; i <= min(n, maxFactor[factorSize + 1]); i++)
            {
                if (n % (i + 1) == 0)
                {
                    dfsFactor(n, i, factor, ans, factorSize);
                    factor.clear();
                }
            }
            sort(ans.begin(), ans.end(), cmp);
            cout << ans[0].size() << endl;
            for (int i = 0; i < ans[0].size(); i++)
            {
                cout << ans[0][i];
                if (i != ans[0].size() - 1)
                    cout << "*";
            }
            cout << endl;
        }
    return 0;
}
时间: 2024-08-25 21:44:28

1096. Consecutive Factors (20)——PAT (Advanced Level) Practise的相关文章

1005. Spell It Right (20)——PAT (Advanced Level) Practise

题目信息: 1005. Spell It Right (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English. Input Specificat

1015. Reversible Primes (20) ——PAT (Advanced Level) Practise

题目信息: 1015. Reversible Primes (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73

1008. Elevator (20)——PAT (Advanced Level) Practise

题目信息: 1008. Elevator (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator w

1001. A+B Format (20) ——PAT (Advanced Level) Practise

题目信息: 1001. A+B Format (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than

1088. Rational Arithmetic (20)——PAT (Advanced Level) Practise

题目信息 1088. Rational Arithmetic (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input fi

1011. World Cup Betting (20)——PAT (Advanced Level) Practise

题目信息: 1011. World Cup Betting (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing

PAT 1096. Consecutive Factors (20)

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

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c

PAT Advanced 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 maxim