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;
}
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页