题目

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. 经验分享:如何搞定Personal Statement?

    最近又到申请季啦,如何自己DIY申请,如何准备文书成为众多留学生关心的问题.不管是你申请本科,硕士,还是博士,相信这篇文章都能帮助到你.下面来说一下文书中一个很重要的组成,就是个人陈述Personal ...

  2. 使用Kali中的Metasploit生成木马控制Windows系统 (第九天 9.20)

    本文转自:https://www.cnblogs.com/yankaohaitaiwei/p/11556921.html 一.kali及Metasploit kali基于debin的数字取证系统,上面 ...

  3. idea修改web项目的访问路径

    转 新建好了项目发现项目只能以localhost:8080这样的访问路径访问到主页,也就是index.jsp 那么之前我用eclipse新建的项目都是localhost:8080/xxx(项目名称)来 ...

  4. Struts1 的配置文件总结

    一.在web.xml中安装Struts 要想使用Struts,我们接触到的第一个配置文件就是web.xml.实际上,Struts的入口点是一个名为ActionServlet的Servlet.在第一次访 ...

  5. centos7+nginx+php+mysql环境搭建

    一:CentOS7安装 在VMware 新建一个虚拟机CentOS 64位,配置好磁盘大小为30G,内存2G,启动虚拟机进入CentOS安装界面 选择Install CentOS 7 SOFTWARE ...

  6. ucosiii 学习笔记

    OSTaskCreate 的参数中有一个 void *p_ext,这个参数用来传递额外的信息,可以用来传递数组,结构体等等. AppTaskStart --> CPU_Init --> C ...

  7. git使用散记

    1.从远程clone一个项目 git clone ‘项目地址’ //clone项目地 git checkout -b dev origin/dev //远程已有dev分支,新建本地dev分支与远程相对 ...

  8. 使用Docker构建基于centos7镜像的python环境

    Dcokerfile配置信息 ############################################## # 基于centos7构建python3运行环境 # 构建命令: 在Dock ...

  9. <kotlin>基础,杂七杂八(亲测有效)

    okhttp class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) ...

  10. Neo4j--节点的增删查改基本用法

    注 node-name 和  label-name node-name 有点句柄的味道. 从面向对象来理解,label-name相当于一个类,node-name相当于这个类的对象. 类比关系型数据库的 ...