一个正整数 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| //将[2,sqrt(n)]内的每一个数均作为一次起点,遍历所有情况 #include<bits/stdc++.h> using namespace std; int len=0,start=2;//len记录长度,start记录开始位置 int main() { int n; scanf("%d",&n); for(int i=2; i<=sqrt(n); i++) { int mid=n,j=i;//以i为起点,找到符合条件的最长因子 while(mid%j==0&&mid) mid/=j,j++; if(j-i>len)//为最长则标记 len=j-i,start=i; } //存在因子 if(len) printf("%d\n",len); for(int i=start; i<start+len; i++) { if(i!=start) printf("*"); printf("%d",i); } //不存在因子 if(len==0) printf("1\n%d",n); return 0; }
|