题意:

给出一个正整数N,找到最长的连续的可分解因子。如N=630,可被分解为630=3*5*6*7,其中5*6*7是3个连续的因子。

思路:

首先,需要明确,对于任何一个整数,如果它是素数,则不可被分解,因子只有1和其本身;如果它是合数,则除了1和本身之外,它的因子必然是在sqrt(n)两侧成对出现的,此时,这些质因子要么全部小于等于sqrt(n);要么只存在一个质因子大于sqrt(n),而其他质因子全部小于sqrt(n)。因此我们只需要考虑2~sqrt(N)的范围即可。对于每一个i∈[2,sqrt(N)],如果N能够被i整除,则记录当前这个i为连续因子的起点,并不断累加直到N不能被整除为止。

代码:

#include <cstdio>
#include <cmath>

int main()
{
    int n;
    scanf("%d",&n);
    int sqr=sqrt(n);
    ;
    ;i<=sqr;i++){
        ) {
            int temp=n;
            int tmpFirst=i,tmpLast=i;
            ){
                temp/=tmpLast;
                tmpLast++;
            }
            if(tmpLast-tmpFirst>len){
                len=tmpLast-tmpFirst;
                first=tmpFirst;
            }
        }
    }
    ) printf("1\n%d",n);//在[2,sqrt(n)]不存在能整除N的连续整数,说明是素数,输出其本身
    else {
        printf("%d\n%d",len,first);
        ;i<first+len;i++)
            printf("*%d",i);
    }
    ;
}

1096 Consecutive Factors的更多相关文章

  1. PAT 1096 Consecutive Factors[难]

    1096 Consecutive Factors (20 分) Among all the factors of a positive integer N, there may exist sever ...

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

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

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

  4. 1096. Consecutive Factors (20)

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

  5. PAT 甲级 1096 Consecutive Factors

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

  6. PAT 1096. Consecutive Factors

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

  7. PAT Advanced 1096 Consecutive Factors (20) [数学问题-因子分解 逻辑题]

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

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

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

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

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

随机推荐

  1. scala学习手记19 - Option类型

    看到Option类型就知道这本教材应该要说那个了. 使用过guava后,应该知道guava中的Optional类的作用是什么.算了找下原始文档好了: Optional<T> is a wa ...

  2. 认证和授权(Authentication和Authorization)

    什么是OAuth 如今很多网站的功能都强调彼此间的交互,因此我们需要一种简单,标准的解决方案来安全的完成应用的授权,于是,OAuth应运而生,看看官网对其的定义: An open protocol t ...

  3. Android调用系统相机拍照保存照片很小解决方案

    保存图片小的一般操作步骤: 1. 调用系统相机 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityFo ...

  4. hdu 5974 A Simple Math Problem

    A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  5. 教你如何使用node.js制作代理服务器

    var http=require("http"); var url=require("url"); var server=http.createServer(f ...

  6. 异步请求(ajax,http) 之 逐渐完善的大全

    异步请求在我们的开发之中是经常需要学习和理解的内容,我们将会在这一篇文章中依据不同的语言和环境内容进行归类讲解. JS: ajax是我们最为常用的页面异步请求,在只需要修改部分页面内容而不需要更换全部 ...

  7. MVC框架中的值提供机制(一)

    在MVC框架中action方法中的Model数据的绑定的来源有很多个,可能是http请求中的get参数或是post提交的表单数据,会是json字符串或是路径中的相关数据;MVC框架中针对这些不同的数据 ...

  8. Win7系统搭建WiFi热点详细攻略

    (转自:http://blog.csdn.net/gisredevelopment/article/details/16113889) 一.如果你之前没有在笔记本上搭建过WiFi,那么恭喜你,你的笔记 ...

  9. L129

    Iraq Sees Spike in Water-Borne IllnessesIraqi health officials say that a health crisis stemming fro ...

  10. 用函数式编程,从0开发3D引擎和编辑器(一)

    介绍 大家好,欢迎你踏上3D编程之旅- 本系列的素材来自我们的产品:Wonder-WebGL 3D引擎和编辑器 的整个开发过程,探讨了在从0开始构建3D引擎和编辑器的过程中,每一个重要的功能点.设计方 ...