A1096 Consecutive Factors [因子分解]

在这里插入图片描述

#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<time.h>
#include<math.h>
using namespace std;
typedef long long ll;
int main()
{
	int n; cin >> n;
	int sqr = sqrt(n), length = 0, k = 0;
	if (n >= 4)
	{
		for (int i = 2; i <= sqr; i++)
		{
			int temp = 1, j = i;
			while (1)
			{
				temp *= j;
				if (n % temp != 0) break;
				if (j - i + 1 > length)
				{
					k = i;
					length = j - i + 1;
				}
				j++;
			}
		}
	}
	else
	{
		for (int i = 2; i <= n; i++)
		{
			int temp = 1, j = i;
			while (1)
			{
				temp *= j;
				if (n % temp != 0) break;
				if (j - i + 1 > length)
				{
					k = i;
					length = j - i + 1;
				}
				j++;
			}
		}
	}
	if (length == 0) //质数
	{
		cout << "1" << endl;
		cout << n << endl;
	}
	else
	{
		cout << length << endl;
		for (int i = 0; i < length; i++)
		{
			cout << k+i;
			if (i < length - 1)
				cout << "*";
		}
	}
}
posted @ 2020-07-16 12:50  _Hsiung  阅读(59)  评论(0编辑  收藏  举报