题意:

给出一个正整数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. 开发中Dialog多弹窗管理

    随着项目的不断迭代,加上产品经理大法(这里加一个弹窗提示,这里加一个引导….)各种弹窗在应用启动时候需要展示, 然而它们出现的时机还有可能重叠.我勒个擦...有没有一种优(tou)雅(lan)的方式来 ...

  2. Mac 终端 Termial 高亮配置(PS1变量配置)

    操作环境: 系统:Mac 10.12 编辑器:vim 一.无脑配置: 1. 打开中端输入: vi ~/.bash_profile 2. 打开并编辑 .bash_profile 文件: 按键盘“i”,进 ...

  3. 【Python那些事儿之十】range()和xrange()

    by Harrison Feng in Python 无论是range()还是xrange()都是Python里的内置函数.这个两个内置函数最常用在for循环中.例如: >>> fo ...

  4. 控制语句1:真假与if 语句

    一.真假与运算符 1.1 真假的划分.查看 任何数据都可以分为两类:True 与 False False : 0,None,空的数据结构例如:[] ,{},str1 = '' True  :除了上面情 ...

  5. 【hdu1705】Count the grid(皮克定理)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1705 [题意] 给出平面上三个点坐标,求围成的三角形内部的点数 做这道题需要先了解下皮克定理. 百度百科: ...

  6. Foundations of Qt Development 学习笔记 Part1 Tips1-50

    1. 信号函数调用的时候仅仅会发送出信号,所以不需要执行 ,所以对于信号声明就行,但是不需要进行定义. 2. 只有槽函数可以声明为public,private,或者是protected的,而信号不行. ...

  7. vs中: 错误,未定义的标识符getline 的解决方法

    这种情况一般都是,在使用的时候没有include<string>而导致的,加上就可以正确编译通过

  8. 点击bindingNavigatorAddNewItem 关联的dataGridView不会新增一行

    方法一. 在设计界面,修改bindingNavigator1的属性AddNewItem 为“(无)”: 方法二. 在设计器自动生成的代码中找到这一行:             // bindingNa ...

  9. 测试中认识 sqlite

    1.SQLite,是一款轻型的数据库:简单, 轻松的API 单词速记中单词离线包也用到sqlite 百度了一下,基本的使用语句: .help .quit sqlite3 testDB.db 在当前目录 ...

  10. Java设计模式百例 - 观察者模式

    观察者(Observer)模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象,主体对象的状态变化会通知所有观察者对象.观察者模式又叫做发布-订阅(Publish/Subscribe ...