PAT (Advanced Level) Practice_1096 Consecutive Factors (20分)(C++_模拟)

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

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.

Sample Input:

630

Sample Output:

3
5*6*7

Code

#include
using namespace std;
#define LL long long 
LL num;
int main()
{
	int maxn = 0, maxa = 0;
	cin >> num;
	for (int i = 2; i <= sqrt(num); i++)
	{
		int j = i, flag = 0, temp = num;
		while (temp % j == 0) {
			temp /= j;
			flag++;
			j++;
		}
		if (maxn < flag) {
			maxn = flag;
			maxa = i;
		}
	}
	if (maxn > 0) {
		cout << maxn << endl << maxa;
		for (int i = 1; i < maxn; i++)
			cout << "*" << maxa + i;
	}
	else
		cout << 1 << endl << num;
	return 0;
}

你可能感兴趣的:(PAT_Advanced,Level)