题意:

给出一个正整数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. asp.net core mvc中如何把二级域名绑定到特定的控制器上

    由于公司的工作安排,一直在研究其他技术,所以一直没时间更新博客,今天终于可以停下手头的事情,写一些新内容了. 应用场景:企业门户网站会根据内容不同,设置不同的板块,如新浪有体育,娱乐频道,等等.有的情 ...

  2. 在阿里云上安装python3.4和pycharm

    一. 安装python3.4 二. 安装pycharm 三. 安装可视化界面和远程桌面连接 四. 启动和配置pycharm 五. 安装更多字体 六. 给pycharm设置桌面快捷方式 一. 安装pyt ...

  3. ASP.NET Core 简单引入教程

    0.简介 开源.跨平台 1.环境安装 参考官方教程   Core 官方文档 2.向世界问个好 sheel/cmd 下: dotnet --help // 查看帮助 dotnet new *     / ...

  4. 【转】Symstore 详细使用

    SymStore (symstore.exe) 是用于创建符号存储的工具.它被包含在Windows调试工具包中. SymStore按照某种格式存储符号,使得调试器可以通过时间戳.映像大小(对于.dbg ...

  5. Python基础学习(第8天)

    先补充些iter函数的用法:iter()其实就是一个迭代器,参数可传个list.dict等等,然后可通过调用next函数获取下一个元素,默认并未指向对象的第一个元素,可理解为指向了第一个元素的前面的位 ...

  6. .Net WebApi 添加Swagger

    前言 随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染.先后端分离的形态,而且前端技术和后端技术在各自的道路上越走越远. 前端和后端的唯一联系,变成了API接口:API文档 ...

  7. 【转载】VC获取MAC地址的4种方法

    From:http://blog.csdn.net/pdfmaker/article/details/465748 有需求才有创造,有了问题才会想着去解决,那么我这里的获取MAC地址的第4种方法也是在 ...

  8. python学习之数据结构

    python的数据很丰富,所以对于数据分析来讲, python是一种最合适的选择 下面讲述一下常见的数据结构,包括栈,队列,元组,字典,集合等,以及对这些数据结构进行操作 #堆栈,后进先出 a=[10 ...

  9. phpcms URL修改

    修改caches\configs\system.php中'html_root' => 'html',//生成静态文件路径改成'html_root' => '',//生成静态文件路径,然后修 ...

  10. 【英语】Bingo口语笔记(86) - stand系列