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 ...
随机推荐
- 继续学习C:数字进制表示
1. 数字后面跟D表示十进制,如:123D. 2. 数字后面跟B表示二进制,如:10010B. 3. 数字后面跟Q表示八进制,如:652Q. 4. 数字后面跟H表示十六进制,如:2B5H. 把十进制数 ...
- 表达式(exp)
题目大意 给定一个逻辑表达式,求每一个数满足$\in[1,n]$的使的表达式为真的方案数. 题解 题目限制较奇怪且数据范围较小,所以可以考虑直接暴力. 考虑枚举每一个变量一共出现了$k$种数值,再枚举 ...
- Excel文本获取拼音
[说明] 版本:Excel 2010 文件后缀:.xls 有在.xlsb文件下使用未成功.建议使用.xls后缀. 1.调出“开发工具” 步骤:文件-->选项-->自定义功能区-->勾 ...
- ACM学习历程—HDU4415 Assassin’s Creed(贪心)
Problem Description Ezio Auditore is a great master as an assassin. Now he has prowled in the enemie ...
- [转]JS的内存泄露处理
问题: 1.给DOM对象添加的属性是一个对象的引用.范例: var MyObject = {}; document.getElementByIdx_x('myDiv').myProp = MyObje ...
- keepalived基本应用解析
原地址:http://blog.csdn.net/moqiang02/article/details/37921051 概念简单认知: Keepalived:它的诞生最初是为ipvs(一些服务,内核中 ...
- Send Code to evernote by my specify notebook
#coding:utf-8 import sys sys.path.append("lib") import thrift.protocol.TBinaryProtocol as ...
- Poj 3356 ACGT(LCS 或 带备忘的递归)
题意:把一个字符串通过增.删.改三种操作变成另外一个字符串,求最少的操作数. 分析: 可以用LCS求出最大公共子序列,再把两个串中更长的那一串中不是公共子序列的部分删除. 分析可知两个字符串的距离肯定 ...
- VMware VirtualCenter Server service fails to start with the vpxd.log error: ODBC error: (28000) (1017688)
Symptoms If you experience an ungraceful shutdown of the database (for example, because of a power o ...
- Python函数式编程(把函数作为参数传入)
map:接受两个参数(函数,Iterable),map将传入的函数依次作用于Iterable的每个元素,并且返回新的Iterable def f(x): return x*x r = map(f,[1 ...