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 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<).

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
5*6*7

最大连乘因子,我们找的是最大连乘因子,所以,我们从2到sqrt(n)进行遍历,因为永远到不了sqrt(n),

之后进行遍历计算。

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main(){
    long int N, sqrt_n;
    cin >> N;
    sqrt_n = sqrt(N);
    vector<int> m, tmp_v;
    for(int i = 2; i <= sqrt_n; i++){
        int cp_i = i, sum = i;
        while(N % sum == 0 && cp_i <= sqrt_n){
            tmp_v.push_back(cp_i);
            cp_i++;
            sum *= cp_i;
        }
        if(tmp_v.size() > m.size())
            m = tmp_v;
        tmp_v.clear();
    }
    if(m.size() == 0) cout << 1 << endl << N;
    else {
        cout << m.size() << endl;
        for(int i = 0; i < m.size(); i++)
            if(i != m.size() - 1) cout << m[i] << "*";
            else cout << m[i];
    }
    system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/littlepage/p/12233061.html

时间: 2024-08-25 21:44:29

PAT Advanced 1096 Consecutive Factors (20分)的相关文章

PAT甲级——1096 Consecutive Factors (数学题)

本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91349859 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

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 consecu

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

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

PAT 甲级 1096 Consecutive Factors

https://pintia.cn/problem-sets/994805342720868352/problems/994805370650738688 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 con

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

如果是素数直接输出1与素数,否则枚举长度和起始数即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; long long n; bool prime(

PAT甲题题解-1096. Consecutive Factors(20)-(枚举)

题意:一个正整数n可以分解成一系列因子的乘积,其中会存在连续的因子相乘,如630=3*5*6*7,5*6*7即为连续的因子.给定n,让你求最大的连续因子个数,并且输出其中最小的连续序列. 比如一个数可以分解2*3*4*6*7*8,最大的连续个数为3,因为存在两个,输出最小的那个即2*3*4. 首先,一个数如果是合数,那么它的因子必定不会超过sqrt(n)或者sqrt(n)+1.如果为质数,那么只可能为自己,因为题目说了不包括1. 我们先将2~sqrt(n)+1中为n的因子存到factor数组中,

PAT Advanced 1152 Google Recruitment (20 分)

In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the picture below) for recruitment. The content is super-simple, a URL consisting of the first 10-digit prime found in consecutive digits of the natural co

PAT Advanced 1152 Google Recruitment (20分)

In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the picture below) for recruitment. The content is super-simple, a URL consisting of the first 10-digit prime found in consecutive digits of the natural co