PAT甲级 1096 Consecutive Factors (20 分)

\quad 感觉这个20分题挺难的,利用构造连乘来找n最大的能够连成的因子个数。

#include <bits/stdc++.h>
using namespace std;

int main(int argc, char const *argv[])
{
	int n;
	cin >> n;
	int maxn = sqrt(n)+1;
	int first = 0, len = 0, temp;
	for (int i = 2; i <= maxn; ++i)
	{
		temp = 1;
		int j;
		for (j = i; j <= maxn; ++j)
		{
			temp *= j;
			if(n%temp!=0) break;
		}
		if(j-i>len)
		{
			len = j-i;
			first = i;
		}
	}
	if(first==0)
	{
		cout << 1 << endl << n << endl;
	}
	else
	{
		cout << len << endl;
		for (int i = first; i < first+len; ++i)
		{
			cout << i;
			if(i!=first+len-1) cout << "*";
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40438165/article/details/89854337