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 ...
随机推荐
- 《java入门第一季》之类(Object类)
package cn.itcast_01; /* * Object:类 Object 是类层次结构的根类.每个类都使用 Object 作为超类. * 每个类都直接或者间接的继承自Object类. * ...
- Linux0.11 中对地址的管理
个字节,段信息无法直接存放在段寄存器中(段寄存器只有2字节).Intel的设计是段描述符集中存放在GDT或LDT中,而段寄存器存放的是段描述符在GDT或LDT内的索引值(index). Linux中逻 ...
- 说说nio----1
既然说到了nio,就得谈以下几个问题 为什么会出现新io,"旧io"有什么问题吗? ok,一步一步来,先给大家看几个例子: 1单线程的服务器程序 import java.net.* ...
- mysql进阶(二十一)删除表数据
MySQL删除表数据 在MySQL中有两种方法可以删除数据,一种是DELETE语句,另一种是TRUNCATE TABLE语句.DELETE语句可以通过WHERE对要删除的记录进行选择.而使用TRUNC ...
- OpenCV 实现颜色直方图
颜色直方图是在许多图像检索系统中被广泛采用的颜色特征.它所描述的是不同色彩在整幅图像中所占的比例,而并不关心每种色彩所处的空间位置,即无法描述图像中的对象或物体.颜色直方图特别适于描述那些难以进行自动 ...
- 用O_APPEND标志open一个文件,能否用lseek在任意位置读写
结论比较简单,用O_APPEND打开后,write操作是一个原子操作,所以每次都会自动把偏移量移到文件末尾,所以用lseek不能在任意位置write.但是可以用lseek在任意位置开始读.下面用代码测 ...
- 第十八篇 ANDROID的声音管理系统及服务
声音管理系统用来实现声音的输入和输出.声音的控制和路由等功能,包括主和各种音源的音量调节.声音焦点控制,声音外设的检测和状态管理,声音源输入和输出的策略管理.音效的播放.音轨设置和播放.录音设置 ...
- The 15th tip of DB Query Analyzer
The 15th tip of DB Query Analyzer ---- SQL Execute Schedule function is realized in 6.01 Ma Gen ...
- javascript显式类型转换
尽管js可以做许多自动类型转换,但某些时候仍然需要做显示类型转换或为了代码逻辑清晰易读而做显示类型转换. 做显示类型转换最简单的方法就是用Boolean().Number().String()或Obj ...
- 一篇文章带你了解Cloud Native
背景 Cloud Native表面看起来比较容易理解,但是细思好像又有些模糊不清:Cloud Native和Cloud关系是啥?它用来解决什么问题?它是一个新技术还是一个新的方法?什么样的APP符合“ ...