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 ...
随机推荐
- Gym - 100801D:Distribution in Metagonia (数学)
题意:给定一个N,让你把它拆成若干个只含素因子2和3的数之和,且两两之间没有倍数关系,比如10=4+6. 思路:即是2因子的幂递增,3因子的幂递减:或者反之. 对于当前N,我们拆分出的数为num=2^ ...
- hdu 5269 ZYB loves Xor I 分治 || Trie
题目大意: 长度为\(n\)的数组A.求对于所有数对\((i,j)(i \in [1,n],j \in [1,n])\),\(lowbit(A_i xor A_j)\)之和.答案对998244353取 ...
- Ubuntu下locale文件
March 7, 2015 11:44 PM locale文件 关于locale文件的设定 locale 是国际化与本土化过程中的一个非常重要的概念,个人认为,对于中文用户来说,通常会涉及到的国际化或 ...
- 人物-IT-张志东:张志东
ylbtech-人物-IT-张志东:张志东 张志东,广东东莞人,腾讯创办人之一,腾讯高级副总裁兼科技总裁,于1993年取得深圳大学理学学士学位,并于1996年取得华南理工大学计算机应用及系统架构硕士学 ...
- 三、kafka主要配置
1.Broker配置 <ignore_js_op> 2.Consumer主要配置 <ignore_js_op> 3.Producer主要配置 <ignor ...
- 十二:JAVA I/O
输入模式 输出模式 ⑴字节输入流:InputStream ⑵字节输出流:Output ...
- Spring MVC配置详解(3)
一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar.spring-webmvc.ja ...
- go 语言 基础 类型(1)
变量 使用关键字 var定义变量,自动初始化为0值.如果提供初始化值,可省略变量类型,由编译器自动推断. 在函数内部可以使用 := 方式定义变量 func main() { x := 123 } 可一 ...
- python 使用sqlite3
Sqlite是一个轻量级的数据库,类似于Access. 一. 安装 Python 2.5开始提供了对sqlite的支持,带有sqlite3库. 没有sqlite的版本需要去PySqlite主页上下载安 ...
- [ural1132]Square Root(cipolla算法)
题意:求${x^2} \equiv n\bmod p$ 解题关键: 定理:若$a$满足$w = {a^2} - n$是模$p$的二次非剩余,即,${x^2} = w\bmod p$无解,则${(a + ...