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 <iostream> 2 #include <vector> 3 #include <cmath> 4 using namespace std; 5 int N, num = 0, first = -1; 6 int main() 7 { 8 cin >> N; 9 for (int i = 2; i <= (int)sqrt(N*1.0); ++i)//2~根号N 10 { 11 if (N%i == 0) 12 { 13 int nn = 0; 14 for (int j = i; N%j == 0; j*=i+nn)//从i开始的连续数字,确保能连续除下去,而不是除以一个数字 15 ++nn; 16 if (nn > num)//更新最长数字串 17 { 18 first = i; 19 num = nn; 20 } 21 } 22 } 23 if (num == 0)//N就是质数 24 cout << 1 << endl << N << endl; 25 else 26 { 27 cout << num << endl; 28 for (int i = 0; i < num; ++i) 29 cout << first + i << (i == num - 1 ? "" : "*"); 30 } 31 return 0; 32 }