1096 Consecutive Factors
题意:
给出一个正整数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的更多相关文章
- PAT 1096 Consecutive Factors[难]
1096 Consecutive Factors (20 分) Among all the factors of a positive integer N, there may exist sever ...
- PAT甲级——1096 Consecutive Factors (数学题)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91349859 1096 Consecutive Factors ...
- 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 ...
- 1096. Consecutive Factors (20)
Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...
- PAT 甲级 1096 Consecutive Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805370650738688 Among all the factors ...
- PAT 1096. Consecutive Factors
Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...
- PAT Advanced 1096 Consecutive Factors (20) [数学问题-因子分解 逻辑题]
题目 Among all the factors of a positive integer N, there may exist several consecutive numbers. For e ...
- PAT (Advanced Level) 1096. Consecutive Factors (20)
如果是素数直接输出1与素数,否则枚举长度和起始数即可. #include<cstdio> #include<cstring> #include<cmath> #in ...
- PAT甲题题解-1096. Consecutive Factors(20)-(枚举)
题意:一个正整数n可以分解成一系列因子的乘积,其中会存在连续的因子相乘,如630=3*5*6*7,5*6*7即为连续的因子.给定n,让你求最大的连续因子个数,并且输出其中最小的连续序列. 比如一个数可 ...
随机推荐
- PHP 环境搭建(win7+php5.6+apache或nginx)
安装介质 PHP5.6.16(php-5.6.16-Win32-VC11-x64.zip) Apache2.4.18(httpd-2.4.18-win64-VC14.zip) nginx (nginx ...
- 7z压缩gopath的src的批处理
7zGoPath.bat @echo off pushd "%~dp0" :config for /f "delims=" %%t in ('powershel ...
- 亚马逊EC2
亚马逊EC2编辑 本词条缺少信息栏,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 亚马逊弹性计算云(EC2,Elastic Compute Cloud)是一个让使用者可以租用云端电脑运行所需 ...
- WCF基础:绑定(二)
在WCF的绑定体系中,经常会碰到ICommunicationObject接口,无论是IChannel接口还是IChannelListener/IChannelFactory接口都继承了ICommuni ...
- C#-foreach与yield
(转自:http://www.jb51.net/article/34627.htm) 1. foreach语句 C#编译器会把foreach语句转换为IEnumerable接口的方法和属性. fore ...
- .SourceInsight添加.S文件
在Option->Document Option添加配置.S然后再去添加文件
- js 取任意两个数之间的随机整数
function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Mat ...
- WeChat on Web 部分功能模拟实现
Flask from flask import Flask,request,render_template,session,jsonify import time import requests im ...
- Activity Process Task Application 专题讲解
Activity Process Task Application 专题讲解 Activity.和进程 为了阅读方便,将文档转成pdf http://files.cnblogs.com/franksu ...
- Visual Studio 2013中用VSIX创建项目模版
我会一步一步解释: 我的一个项目里有5个工程, 我想用其中的4个去创建一个新的工具模版,我还想把他们都放到一个文件夹下面当我用这个模版创建新的工程时.注意我的初始化项目,我想把他们转换成一个模版(我不 ...