Consecutive Factors (20)

暴力求解。

#include "iostream"
#include "vector"
#include "string"
#include "cstring"
#include "queue"
#include "map"
using namespace std;

int main()
{
	int N;
	while (cin >> N)
	{
		int max = 1;
		int s = sqrt(N);
		int startNum = N;
		for (int i = 2; i <= s; i++)
		{
			if (N%i == 0)
			{
				int len = 1;
				int tempN = N;
				tempN /= i;
				while (tempN % (++i) == 0)
				{
					tempN /= i;
					++len;
				}
				if (len > max)
				{
					max = len;
					i = i - len;
					startNum = i;
				}
				else if(len == max && i < startNum)
				{
					i = i - len;
					startNum = i;
				}
			}
		}
		cout << max;
		for (int i = 0; i < max - 1; i++)
		{
			cout << startNum++ << '*';
		}
		cout << startNum;
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章