题目描述
一个正整数 N 的因子中可能存在若干连续的数字。例如 630 可以分解为 3×5×6×7,其中 5、6、7 就是 3 个连续的数字。给定任一正整数 N,要求编写程序求出最长连续因子的个数(其本身也算其因子),并输出最小的连续因子序列。
输入
输入在一行中给出一个正整数 N(1<N<2^31)。
输出
首先在第 1 行输出最长连续因子的个数;然后在第 2 行中按 因子1因子2……*因子k 的格式输出最小的连续因子序列,其中因子按递增顺序输出,1 不算在内。
样例输入
630
样例输出
3
567
直接枚举就行
#include<bits/stdc++.h>
using namespace std;
int main(){
long long n;
int ans = 0;
int b = 0, e = 0;
cin >> n;
for(int i = 2; i <= sqrt(n); i++){
if(n % i == 0){
int count = 0; //记录次数
int a = i;
int s = n;
while(s%a == 0){ //循环 连续因子
s = s/a;
a++;
count++;
}
if(count > ans){
ans = count;
b = i;
e = a-1;
}
}
}
if(ans == 0){
cout << 1 << endl;
cout << n;
} else{
cout << ans <<endl;
for(int i = b; i < e; i++){
cout << i << "*";
}
cout << e;
}
}