0
点赞
收藏
分享

微信扫一扫

C++解PTA A1096Conse

秦瑟读书 2022-01-23 阅读 26
c++

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

核心思路

要完整模拟整个过程,建议数据要长整型。

完整代码

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
    //亮点,长质数
    ll N;
    cin >> N;
    ll i,j,k;
    ll ans_start = N;
    ll ans_length = 1;
    for(i=2;i<=sqrt(N);i++){
        for(j=i,k=N;k%j==0;j++){
            k/=j;
        }
        if(j-i>ans_length||(j-i==ans_length&&i<ans_start)){
            ans_length =j-i;
            ans_start = i;
        }
    }
    cout << ans_length << endl;
    for(i=ans_start;i<ans_start+ans_length;i++){
        if(i>ans_start)cout << "*";
        cout << i;
    }
}


举报

相关推荐

C++精解【6】

C++精解【8】

C++题解PTA A1028 List

PTA 睡前速刷(C++ & Java)

PTA乙级-1012 数字分类 c++

C++解PAT A1048 Find C

c++解PAT A10878Hashing

0 条评论