PAT1096:Consecutive Factors
1096. Consecutive Factors (20)
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<231).
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 思路
因为12! < 2^31 < 13!。
所以,因子大小不会超过12!,连续因子的长度不会超过12。
从长度为12开始减小,令res为满足条件的最大因子,穷举所有可能直到满足N % res == 0就行。
注:穷举的过程中res会溢出,不过不影响结果。。
代码
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int N;
while(cin >> N)
{
int maxnum = sqrt(N);
for(int len = 12;len >= 1;len--)
{
for(int start = 2; start <= maxnum;start++)
{
int res = 1;
for(int i = start;i - start < len;i++)
{
res *= i;
}
if(N % res == 0)
{
cout << len << endl << start;
for(int j = start + 1;j - start < len;j++)
cout << "*" << j;
return 0;
}
}
}
cout << 1 << endl;
cout << N;
}
}
PAT1096:Consecutive Factors的更多相关文章
- 1096. Consecutive Factors (20)
Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...
- PAT A1096 Consecutive Factors (20 分)——数字遍历
Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...
- A1096. Consecutive Factors
Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...
- PAT 甲级 1096 Consecutive Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805370650738688 Among all the factors ...
- PAT 1096 Consecutive Factors[难]
1096 Consecutive Factors (20 分) Among all the factors of a positive integer N, there may exist sever ...
- PAT甲级——1096 Consecutive Factors (数学题)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91349859 1096 Consecutive Factors ...
- 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 ...
- PAT_A1096#Consecutive Factors
Source: PAT A1096 Consecutive Factors (20 分) Description: Among all the factors of a positive intege ...
随机推荐
- SpriteBuilder中不能编辑自定义类或不能给节点添加属性的解决
不能编辑自定义类 你选中一个Sub File(CCBFile)节点,在这个例子中,该节点的Custom class区域灰化禁用且不能修改.这是因为你需要在该Sub File引用的CCB文件中修改Cus ...
- 如何让你的传输更安全——NIO模式和BIO模式实现SSL协议通信
对于SSL/TLS协议,如果要每个开发者都自己去实现显然会带来不必要的麻烦,正是为了解决这个问题Java为广大开发者提供了Java安全套接字扩展--JSSE,它包含了实现Internet安全通信的一系 ...
- Leetcode_137_Single Number II
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42877129 Given an array of inte ...
- STL算法设计理念 - 二元函数,二元谓词以及在set中的应用
demo 二元函数对象 #include <iostream> #include <cstdio> #include <vector> #include <a ...
- ITU-T Technical Paper: IP网络测量模型
本文翻译自ITU-T的Technical Paper:<How to increase QoS/QoE of IP-based platform(s) to regionally agreed ...
- Linux - test测试标志的意思总结
测试的标志 代表意义 1. 关於某个档名的『文件类型』判断,如 test -e filename 表示存在否 -e 该『档名』是否存在?(常用) -f 该『档名』是否存在且为文件(file)?(常用) ...
- Linux打包命令 - tar
上一篇文章谈到的命令大多仅能针对单一文件来进行压缩,虽然 gzip 与 bzip2 也能够针对目录来进行压缩, 不过,这两个命令对目录的压缩指的是『将目录内的所有文件 "分别" 进 ...
- PhotoShop 图像处理 算法 汇总
不定期更新 ...... 直接点标题即可链接到原文. OpenCV 版:OpenCV 图像处理 图层混合算法: PS图层混合算法之一(不透明度,正片叠底,颜色加深,颜色减淡)PS图层混合算法之二(线性 ...
- HBase BlockCache
1. Cache 读写 调用逻辑: hmaster.handleCreateTable->HRegion.createHRegion-> HRegion. initialize-> ...
- Oracel 编写控制结构
1.条件分支语句 在Oracle9i之前,执行条件分支操作都需要使用IF语句来完成,并且PL/SQL中,提供了三种条件分支语句:IF-THEN.IF-THEN-ELSE.IF-THEN-ELSIF.具 ...