PAT甲级 1096
题目 Consecutive Factors
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.
解析
输入一个数,获取该数能连续被除的最长序列
代码
#include<bits/stdc++.h>
using namespace std;
int n;
vector<int> data,res;
int main(){
scanf("%d", &n);
int max=0;
int sqr = sqrt(n);
int i,j;
for(i=2;i<=sqr;i++){
if(n%i==0){
data.push_back(i);
}
}
for(i = 0;i<data.size();i++){
int temp = data[i];
int temp_max=0;
int temp_n=n;
vector<int> temp_res;
for(j = i;j<data.size();j++){
if(temp == data[j] && temp_n%temp == 0){
temp_max++;
temp_res.push_back(temp);
temp_n/=temp;
temp++;
}else{
break;
}
}
if(temp_max > max){
res = temp_res;
max = temp_max;
}
}
if(res.size()!=0){
printf("%d\n",res.size());
for(i=0;i<res.size();i++){
printf("%d", res[i]);
if(i!=res.size()-1)
printf("*");
}
}else{
printf("1\n");
printf("%d",n);
}
}