A1096. Consecutive Factors
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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int main(){
int N, sqr, maxLen = -, index;
long long P;
scanf("%d", &N);
sqr = (int)sqrt(N * 1.0);
for(int i = ; i <= sqr + ; i++){
int j = i;
P = ;
while(){
P *= j;
if(P < )
break;
if(N % P != )
break;
if(j - i + > maxLen){
maxLen = j - i + ;
index = i;
}
j++;
}
}
if(maxLen != -){
printf("%d\n", maxLen);
printf("%d", index++);
}else{
printf("%d\n%d", , N);
}
for(int i = ; i < maxLen; i++){
printf("*%d", index++);
}
cin >> N;
return ;
}
总结:
1、测试630的序列,应该是测试 630 / 3, 630 /(3 * 4), 630 / (3 * 4 * 5),而非测试630 / 3,630 / 4, 630 / 5。
2、 考虑到无解的可能,比如素数13, index 与maxlen没有进入循环,应单独输出。
3、有可能sqrt(N)后有误差,应该判断到 sqrt(N + 1)而非 sqrt(N)。
4、只要有累乘,就应该用long long,并且考虑到溢出为负的可能。
A1096. Consecutive Factors的更多相关文章
- PAT A1096 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 ...
- PAT_A1096#Consecutive Factors
Source: PAT A1096 Consecutive Factors (20 分) Description: Among all the factors of a positive intege ...
- 1096. Consecutive Factors (20)
Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...
- PAT1096:Consecutive Factors
1096. Consecutive Factors (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- 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 ...
随机推荐
- Individual P1: Preparation
Individual Project - Word frequency program tally the frequency of words under a directory (2 modes) ...
- Alpha冲刺之事后诸葛亮
组长博客 作业博客 项目Postmortem 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件针对的是福大学子来到食堂会犹豫不决无法决定吃什么 ...
- HDOJ2032_杨辉三角
这是一道水题,思路很简单,把杨辉三角先求出来,然后按照输入将相应的层数的杨慧三角输出即可. HDOJ2032_杨辉三角 #include<stdio.h> #include<stdl ...
- the confilict between validation and readonly in jquery
How can I disable jquery validation on readonly fields? http://stackoverflow.com/questions/10803294/ ...
- cxGrid导出Excel货币符号问题
cxGrid导出到Excel,对于Currency类型总是加上了货币符号,可以修改导出文件设置来去掉: 在cxXLSExport.pas文件中,修改: procedure TcxXLSExportPr ...
- python matplotlib绘图
import numpy as np import matplotlib.pyplot as plt from scipy.constants.constants import alpha from ...
- apache2.4.29安装脚本
#!/bin/bash . /etc/init.d/functions yum install -y expat-devel gcc gcc-c++ zlib-devel openssl-devel ...
- 关于一个常用的CheckBox样式
我们在使用CheckBox的时候,原始的样式有时不能满足我们的需求,这是我们就需要更改其模板,比如我们常用的一种,在播放器中“播放”.“暂停”按钮,其实这也是一种CheckBox,只不过我们只是修改了 ...
- PostgreSQL字段类型说明
BIGSERIALSERIAL8 存储自动递增的惟一整数,最多 8 字节. BIT 固定长度的位串. BIT VARYING(n)VARBIT(n) 可变长度的位串,长度为 n 位. BOOLEAN ...
- codeforces445A
DZY Loves Chessboard CodeForces - 445A DZY 喜欢棋盘,他很享受棋盘上的游戏. 他有一个 n 行和 m 列的棋盘.棋盘上的某些单元格是坏的位置,其他的是好的位置 ...