当前位置:网站首页>PAT (Advanced Level) 1096——Consecutive连续的
PAT (Advanced Level) 1096——Consecutive连续的
2022-04-21 18:55:00 【Coco_T_】
- 数据范围使用int即可
- 求因数是sqrt级别的复杂度,简单暴力就可以了
- 质数需要特殊处理
我一开始无意识地写出了求因数的代码(每个数单独判断是否可以整除)
但是题目的意思是,这一连串因数需要乘起来
即再计算最长连续因数序列时,需要积累各个因数的影响
a /= (cnt + i);
我还想当然的认为,连续因数有这样的性质:若检测过 [ a , b ] [a,b] [a,b]这个序列,那么之后我们只需要从 b + 1 b+1 b+1开始检测即可
这是一丁点都错的!
所以我们要对从2到sqrt的每一个数i,都计算以i为起点的最长连续因数序列
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
int x;
int main()
{
scanf("%d", &x);
int cnt = 0;
int ans_cnt = 0;
int begin;
for (int i = 2; i <= sqrt(x);++i) {
if (x % i == 0) {
int a = x / i;
cnt = 1;
while (a % (cnt + i) == 0) {
a /= (cnt + i);
cnt++;
}
if (cnt > ans_cnt) {
ans_cnt = cnt;
begin = i;
}
}
}
if (ans_cnt == 0) {
printf("1\n%d",x);
}
else {
printf("%d\n", ans_cnt);
for (int i = 0; i < ans_cnt; ++i) {
printf("%d", begin + i);
if (i < ans_cnt - 1) printf("*");
}
}
return 0;
}
版权声明
本文为[Coco_T_]所创,转载请带上原文链接,感谢
https://blog.csdn.net/wu_tongtong/article/details/124317747
边栏推荐
猜你喜欢

Tencent cloud database tdsql -- blog database migration practice

Semaphore and exchange of concurrent tools

Typescript quick start, class, public, private, extensions

【js学习笔记四十】复杂工厂模式

序列化对象+Properties+IO框架

大厂推荐使用的网关解密:Fizz Gateway后台管理系统功能模块介绍

无线蓝牙耳机哪个品牌好?无线蓝牙耳机推荐

Abbexa 山羊 F(ab‘)2 IgG 同种型对照

mysql不能使用 mysql -u root -p 启动报错解决

CheKine 系列高密度脂蛋白(HDL-C)含量检测方案
随机推荐
为什么你做数据分析没思路?
一针见血式处理doGet&doPost中文乱码问题
Abbexa MPO (FITC) / CD3 (PE) 组合抗体
重大活动评论狂欢活动链路建设
669. 修剪二叉搜索树
Mysql database learning - Chapter 6 post class exercises of multi table query
Ubuntu install go
flink分流
SQL data type
SVG系列——1,入门级操作
Semaphore and exchange of concurrent tools
天创速盈简述静态创意是怎么收费的呢?
手把手教會你搭建組件與應用
What's a good brand of real wireless headphones? High profile flagship Bluetooth headset
leetcode:423. 从英文中重建数字
Abbexa 驴抗山羊 IgG (H&L) 抗体
【王道考研3】OSI七层参考模型,TCP/IP参考模型和5层参考模型
编程中的Context(上下文)
CheKine 系列高密度脂蛋白(HDL-C)含量检测方案
使用MCUXpresso开发RT1060(1)——驱动RGB接口LCD