PAT甲级——1096 Consecutive Factors (数学题)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91349859
Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×5×6×7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.
Input Specification:
Each input file contains one test case, which gives the integer N (1<N<).
Output Specification:
For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format factor[1]*factor[2]*...*factor[k]
, where the factors are listed in increasing order, and 1 is NOT included.
Sample Input:
630
Sample Output:
3
5*6*7
题目大意:一个数字可以写成若干因子相乘的形式,在这些因子中寻找连续的数字形成一个序列,使得此序列的成员个数最多,输出成员个数最多且数值最小的序列,写成相乘的格式。
思路:直接从2遍历到sqrt( N ) +1,找能整除N的连续乘积,此乘积的连续数字存入tmpAns数组中,size最大的那组序列就是答案。
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int N, x;
vector <int> ans, tmpAns;
int main()
{
int i, product = ;
scanf("%d", &N);
x = sqrt(N) + ;
/*寻找最多的连续的数字,连续相乘的积能整除N*/
for (i = ; i <= x; i++) {
product *= i;
tmpAns.push_back(i);
if (product <= N && N % product == && tmpAns.size() > ans.size())//乘积小于N且能整除N又是目前找到的最多连续数字
ans = tmpAns;
else if(product > N || N % product != ) {//乘积大于N或者新加入的i使得乘积无法整除N
tmpAns.clear();
if (N % i != )//新加入的i不能整除N,将product初始化
product = ;
else {//新加入的i是N的因子,意味着product的值超过了N,那么从前面开始删除节点,使得product小于N
int tmpPro = product, j;
for (j = ; j <= i; j++) {
tmpPro = tmpPro / j;
if (tmpPro <= N && N % tmpPro == ) {
product = tmpPro;
break;
}
}
for (j++; j <= i; j++)
tmpAns.push_back(j);
}
}
}
/*N为素数的时候输出它本身*/
if (ans.size() == )
printf("1\n%d\n", N);
else {
printf("%d\n", ans.size());
for (int i = ; i < ans.size(); i++) {
if (i != )
printf("*");
printf("%d", ans[i]);
}
}
return ;
}
PAT甲级——1096 Consecutive Factors (数学题)的更多相关文章
- PAT 甲级 1096 Consecutive Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805370650738688 Among all the factors ...
- PAT甲级——A1096 Consecutive Factors【20】
Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...
- PAT Advanced 1096 Consecutive Factors (20) [数学问题-因子分解 逻辑题]
题目 Among all the factors of a positive integer N, there may exist several consecutive numbers. For e ...
- PAT 1096 Consecutive Factors[难]
1096 Consecutive Factors (20 分) Among all the factors of a positive integer N, there may exist sever ...
- PAT (Advanced Level) Practise - 1096. Consecutive Factors (20)
http://www.patest.cn/contests/pat-a-practise/1096 Among all the factors of a positive integer N, the ...
- PAT 1096. Consecutive Factors
Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...
- 1096. Consecutive Factors (20)
Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...
- 【PAT甲级】1096 Consecutive Factors (20 分)
题意: 输入一个int范围内的正整数,输出它最多可以被分解为多少个连续的因子并输出这些因子以*连接. trick: 测试点5包含N本身是一个素数的数据,此时应当输出1并把N输出. 测试点5包含一个2e ...
- PAT (Advanced Level) 1096. Consecutive Factors (20)
如果是素数直接输出1与素数,否则枚举长度和起始数即可. #include<cstdio> #include<cstring> #include<cmath> #in ...
随机推荐
- UDEV管理RAC共享存储
背景:操作系统 centos 6.7 数据库:11.2.0.1 操作流程: 1. 确认在所有RAC节点上已经安装了必要的UDEV包[root@11gnode1 ~]# rpm -qa|grep ude ...
- 洛谷【P2005】A/B Problem II
题目传送门:https://www.luogu.org/problemnew/show/P2005 高精除低精:https://www.cnblogs.com/AKMer/p/9724556.html ...
- POJ2528(离散化+线段树区间更新)
Mayor's posters Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u De ...
- POJ3468(线段树区间维护)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 85502 ...
- JAVA 1.5 局部特性(可变参数/ANNOTATION/并发操作)
1: 可变参数 可变参数意味着可以对某类型参数进行概括,例如十个INT可以总结为一个INT数组,当然在固定长度情况下用数组是很正常的 这也意味着重点是可变,不定长度的参数 PS1:对于继承和重写我没有 ...
- oracle--循环PL/SQL--demo1---
--简单的条件判断if–then --编写一个过程,可以输入一个雇员名,如果该雇员的工资低于2000,就给该员工工资增加10%. create or replace procedure sp_pro6 ...
- VisualGDB系列4:概述-Linux程序与VS
根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文将会阐述如何使用VisualGDB来 ...
- C#的闭包
简单的理解:闭包变量是把局部变量的作用域扩展到回调函数,发生在匿名方法注册到委托上,而匿名方法中使用外部的局部变量 说什么都不如图示那么容易明白啊 先看C#的源码 class Program { st ...
- ADO.NET 对象
(一)OleDbConnection 使用COM组件链接 (二)ADO.NET 对对数据库访问做了优化: 1.SqlConnection 用于建立和Sql Server服务器连接的类,表示打开数据库 ...
- 洛谷-机器翻译-NOIP2010提高组复赛
题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英文单词,软件会先 ...