PAT甲级 A1096
题目详情
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
567
解题思路
这道题就不断地遍历存储最大的连续子串即可 需要注意的是,如果没有连续的串就把当前数字输出
以下为AC代码
#include <cstdio>
#include <algorithm>
#include<iostream>
#include<vector>
#include<cmath>
#include<iomanip>
#include<map>
#include<queue>;
using namespace std;
int main() {
int N; cin >> N;
vector<long long int> nos;
for (long long int i = 2;i <= sqrt(N); i++) {
vector<long long int> temps;
long long int cal = i;
long long int t = i;
while (N % cal == 0) {
temps.push_back(t);
t = t + 1;
cal = cal * t;
}
if (temps.size() > nos.size()) {
nos = temps;
}
}
if (nos.size() == 0) {
cout << 1 << '\n';
cout << N << '\n';
return 0;
}
cout << nos.size() << '\n';
for (int i = 0; i < nos.size(); i++) {
if (i != 0) {
cout << "*" << nos[i];
}
else {
cout << nos[i];
}
}
cout << '\n';
}