PAT A1096 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
 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <set>
 4 #include <string.h>
 5 #include <vector>
 6 #include <queue>
 7 using namespace std;
 8 typedef long long ll;
 9 const int maxn = 110;
10 ll n;
11 
12 int main(){
13     scanf("%lld",&n);
14     ll res=0,res_j=0,res_i=0;
15 
16     for(ll i=2;i*i<=n;i++){
17         ll sum=1,j=i;
18         while(1){
19             sum*=j;
20             if(n%sum!=0)break;
21             j++;
22         }
23         j--;
24         if(j-i+1>res){
25             res=j-i+1;
26             res_j=j;
27             res_i=i;
28         }
29     }
30     if(res==0) {
31       printf("1
%lld",n);
32       return 0;}
33     printf("%lld
",res);
34     for(int i=res_i;i<res_j;i++){
35         printf("%lld*",i);
36     }
37     printf("%lld",res_j);
38 }
View Code

注意点:一开始找成了所有因子都连续,结果一直是0,后来发现只要能整除就好了。其实和找质数也差不多

第二点是要用long long 不能用int,虽然题目输入是int范围,但是你的乘积结果会超int,导致死循环。

第三点是遍历上限到sqrt(n)就好了,否则会超时,因为sqrt(n) * (sqrt(n)+1) 肯定大于n了。

第四点是遇到质数要输出自己,不是只判断2和3就好了,只要看res是不是还是0。

---------------- 坚持每天学习一点点
原文地址:https://www.cnblogs.com/tccbj/p/10448494.html