题目

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

567

题目分析

已知一个整数N,求所有N的连续因子因式分解中,最长连续因子个数和最小连续因子序列

注:最小连续因子序列的长度其实就是最长连续因子个数(因为连续因子序列的第一个数越大,连续因子序列越短)

解题思路

  1. 连续因子数

    1.1 若为0,表示N是质数,因子只有1和N本身

    1.2 若大于0

    1.2.1 若等于1,表示N因子分解后,一个因子<=sqr(n),一个因子>=(sqr(n))

    1.2.2 若大于1,表示N因子分解后,可以找到连续因子

易错点

  1. 注意连续因子数为0和连续因子树为1的不同情况区分
  2. 最小连续因子序列的长度其实就是最长连续因子个数(因为连续因子序列的第一个数越大,连续因子序列越短)

Code

#include <iostream>
#include <cmath>
using namespace std;
int main(int argc,char * argv[]) {
int n;
scanf("%d",&n);
int sqr=(int)sqrt(1.0*n);
int len = 0,first=0;
for(int i=2; i<=sqr; i++) {
int temp =1;
int j;
for(j=i; j<=sqr; j++) {
temp*=j;
if(n%temp!=0)break;
}
if(j-i>len) {
len=j-i;
first = i;
}
}
if(len==0) printf("1\n%d", n);
else {
printf("%d\n",len);
for(int i=first; i<first+len; i++) {
if(i!=first)printf("*");
printf("%d", i);
}
}
return 0;
}

PAT Advanced 1096 Consecutive Factors (20) [数学问题-因子分解 逻辑题]的更多相关文章

  1. PAT甲级——1096 Consecutive Factors (数学题)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91349859 1096 Consecutive Factors  ...

  2. 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 ...

  3. 1096. Consecutive Factors (20)

    Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...

  4. PAT 甲级 1096 Consecutive Factors

    https://pintia.cn/problem-sets/994805342720868352/problems/994805370650738688 Among all the factors ...

  5. PAT (Advanced Level) 1096. Consecutive Factors (20)

    如果是素数直接输出1与素数,否则枚举长度和起始数即可. #include<cstdio> #include<cstring> #include<cmath> #in ...

  6. PAT甲题题解-1096. Consecutive Factors(20)-(枚举)

    题意:一个正整数n可以分解成一系列因子的乘积,其中会存在连续的因子相乘,如630=3*5*6*7,5*6*7即为连续的因子.给定n,让你求最大的连续因子个数,并且输出其中最小的连续序列. 比如一个数可 ...

  7. 【PAT甲级】1096 Consecutive Factors (20 分)

    题意: 输入一个int范围内的正整数,输出它最多可以被分解为多少个连续的因子并输出这些因子以*连接. trick: 测试点5包含N本身是一个素数的数据,此时应当输出1并把N输出. 测试点5包含一个2e ...

  8. PAT Advanced 1132 Cut Integer (20) [数学问题-简单数学]

    题目 Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long ...

  9. PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]

    题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...

随机推荐

  1. > 1> 2> &> /dev/null Linux重定向输出

    编译模拟器的 LINK 阶段产生了大量错误信息,定位不到第一行,所以将错误重定向到了一个文件: scons build/X86_VI_hammer_GPU/gem5.opt --default=X86 ...

  2. hive表字段注释显示乱码问题

    创建了一张hive表,对字段增加了注释,比如comment '注释内容' 之类的,但是在hive client查看时候却是乱码 比如: create table test_ultraedit ( id ...

  3. 吴裕雄--天生自然C++语言学习笔记:C++ 数据封装

    所有的 C++ 程序都有以下两个基本要素: 程序语句(代码):这是程序中执行动作的部分,它们被称为函数. 程序数据:数据是程序的信息,会受到程序函数的影响. 封装是面向对象编程中的把数据和操作数据的函 ...

  4. 9 ~ express ~ 用户注册

    一,/public/js/index.js //登陆 $login.find('button').on('click',()=>{ $.ajax({ type:'post', url:'/api ...

  5. poj_2406 KMP寻找重复子串问题

    B - Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  6. PAT Advanced 1018 Public Bike Management (30) [Dijkstra算法 + DFS]

    题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...

  7. FFmpeg命令大全(更新中)

    1.视频抽取音频: ffmpeg -i 3.mp4 -vn -y -acodec copy 3.aacffmpeg -i 3.mp4 -vn -y -acodec copy 3.m4a

  8. k8s安装helm

    1.客户端安装 A.直接在github上下载二进制文件进行解压,下载地址:https://github.com/kubernetes/helm/releases B.将解压出来的二进制文件helm 拷 ...

  9. CSS行间样式与内部样式

    注释 <!-- --> 快捷键 crtl+/ div 双标签<div></div> CSS语法    属性名:属性值: Css引入方式 1.行间样式的引入:在标签中 ...

  10. VS2019企业版产品密钥

    Visual Studio 2019 Enterprise产品密钥(激活码) BF8Y8-GN2QH-T84XB-QVY3B-RC4DF