【PAT-A1096】Consecutive Factors(连续因子, 暴力)

【注意点】

测试点3不过
反例:
n = 234537
当解到2
345后,发现6不行,当前最长的长度为4这里的算法直接从7开始往后。
但是如果从3开始34567是可行的,所以实际忽略了这种情况应该每次只走一步。
我开始写的算法在发现6不行后就直接从7开始了。

【代码】

#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;

int main() {
	LL n;
	LL maxl = 0, cntl = 0, last;
	scanf("%lld", &n);
	LL tmp = n;
	LL i = 2, sqr;
	sqr = (LL)sqrt(1.0 * n);
	while (i <= sqr)
	{
		int j = i;
		while (1)
		{
			if (tmp == 1) break;

			if (tmp % j == 0) {
				cntl++;
				tmp /= j;
			}
			else {
				cntl = 0;
				tmp = n;
				break;
			}

			if (cntl > maxl) {
				maxl = cntl;
				last = j;
			}
			j++;
		}

		i++;

	}

	if (maxl == 0) {
		printf("1\n%d\n", n);
	}
	else
	{
		printf("%d\n", maxl);
		for (LL i = last - maxl + 1; i <= last; i++)
		{
			if (i != last - maxl + 1) printf("*");
			printf("%d", i);
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值