题意:

给出一个正整数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. Oracle cmd乱码

    查看下环境变量的设置,查看是否有变量NLS_LANG,没有则新建该变量.新建变量,设置变量名:NLS_LANG,变量值根据以上字符集确定,一般都是中文简体SIMPLIFIED CHINESE_CHIN ...

  2. idea解决properties乱码问题

    问题:我的IDEA已经将文件的字符集设置成了UTF-8,但是中文在*.properties文件中还是会出现乱码,后来经同事指点修改了一项配置就ok了!话不多说,看下面的对比就清楚了. 设置前: 设置后 ...

  3. C#在winform中操作数据库,实现数据增删改查

    1.前言: 运行环境:VS2013+SQL2008+Windows10 程序界面预览: 使用的主要控件:dataGridview和menuStrip等. 2.功能具体介绍: 1.首先,我们要先实现基本 ...

  4. css11动态效果

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. LeetCode OJ:Combination Sum II (组合之和 II)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  6. springmvc跨域(转)

    跨域资源共享 CORS 详解  原文链接:http://www.ruanyifeng.com/blog/2016/04/cors.html   作者: 阮一峰 日期: 2016年4月12日 CORS是 ...

  7. 【dlbook】数学基础

    [代数] Moore-Penrose 伪逆 [概率信息论] 自信息,香农熵,衡量两个分布的差异:kl散度 \ 交叉熵 [数值] 溢出: softmax计算的时候要关注上溢和下溢,如果所有X都相等且为很 ...

  8. jsp中的session

    浏览器和服务器的异常通话 常用方法 setAttribute(String key,Object value);//设置值 getAttribute(String key); //取值 Invalid ...

  9. 更改Linux栈空间大小

    1.通过命令 ulimit -s 查看linux的默认栈空间大小,默认情况下 为10240 即10M 2.通过命令 ulimit -s 设置大小值 临时改变栈空间大小:ulimit -s 102400 ...

  10. Android学习之Activity跳转与传值

    Activity跳转与传值,主要是通过Intent类,Intent的作用是激活组件和附带数据. 一.Activity跳转 方法一 Intent intent = new Intent(A.this, ...