一个正整数 N 的因子中可能存在若干连续的数字。例如 630 可以分解为 3×5×6×7,其中 5、6、7 就是 3 个连续的数字。给定任一正整数 N,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列。
输入格式:
输入在一行中给出一个正整数 N(1<N<231)。
输出格式:
首先在第 1 行输出最长连续因子的个数;然后在第 2 行中按 因子1* 因子2*……*因子k 的格式输出最小的连续因子序列,其中因子按递增顺序输出,1 不算在内。
输入样例:
630
输出样例:
3
5*6*7
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int isPrime(int n);//判断素数
int main()
{
int n, temp, count = 0, a[20], max = 0, flag;
scanf("%d", &n);
if (isPrime(n))//如果是素数,直接按照题意输出就行了
{
printf("1\n%d", n);
exit(0);
}
for (int i = 2; i <= sqrt(n); i++)
{
temp = n;//每次都必须初始化temp和count
count = 0;
for (int j = i; j <= sqrt(n); j++)
{
if (temp % j == 0)
{
count++;
temp /= j;
}
else
break;
}
if (count > max)
{
max = count;
flag = i;//flag是第一个要输出的数的标记
}
}
printf("%d\n", max);
for (int i = flag; i < flag+max-1; i++)
printf("%d*", i);
printf("%d", flag + max - 1);
return 0;
}
int isPrime(int n)
{
if (n == 1)
return 0;
for (int div = 2; div <= sqrt(n); div++)
if (n % div == 0)
return 0;
return 1;
}