本题要求:
一个正整数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<iostream>
#include<cstring>
#include<cmath>
using namespace std;
#define MAXSIZE 100
int main() {
int n;
cin >> n;
int max = 0;
int m2 = 1;
int time = sqrt(n);
int num[100] = {0};
num[0] = n;
while (m2 < time) {
int temp = n;
int m = 1 + m2;
int tempMax = 0;
int tempNum[100] = {0};
while (temp != 1 && m <= time && m <= MAXSIZE) {
if (temp % m == 0) {
temp /= m;
tempNum[tempMax] = m;
tempMax++;
} else {
if (tempMax > max) {
max = tempMax;
for (int j = 0; j < max; j++) {
num[j] = tempNum[j];
}
}
tempMax = 0;
}
m++;
}
if (tempMax > max) {
max = tempMax;
for (int j = 0; j < max; j++) {
num[j] = tempNum[j];
}
}
m2++;
}
if (max == 0) {
max = 1;
}
cout << max << endl;
for (int j = 0; j < max; j++) {
cout << num[j];
if (j + 1 < max) {
cout << '*';
}
}
cout << endl;
return 0;
}