PAT Advanced 1096 Consecutive Factors (20) [数学问题-因子分解 逻辑题]
题目
Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3*5*6*7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.
Input Specification:
Each input file contains one test case, which gives the integer N (1<N<231).
Output Specification:
For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format “factor[1]*factor[2]*…*factor[k]”, where the factors are listed in increasing order, and 1 is NOT included.
Sample Input:
630
Sample Output:
3
567
题目分析
已知一个整数N,求所有N的连续因子因式分解中,最长连续因子个数和最小连续因子序列
注:最小连续因子序列的长度其实就是最长连续因子个数(因为连续因子序列的第一个数越大,连续因子序列越短)
解题思路
- 连续因子数
1.1 若为0,表示N是质数,因子只有1和N本身
1.2 若大于0
1.2.1 若等于1,表示N因子分解后,一个因子<=sqr(n),一个因子>=(sqr(n))
1.2.2 若大于1,表示N因子分解后,可以找到连续因子
易错点
- 注意连续因子数为0和连续因子树为1的不同情况区分
- 最小连续因子序列的长度其实就是最长连续因子个数(因为连续因子序列的第一个数越大,连续因子序列越短)
Code
#include <iostream>
#include <cmath>
using namespace std;
int main(int argc,char * argv[]) {
int n;
scanf("%d",&n);
int sqr=(int)sqrt(1.0*n);
int len = 0,first=0;
for(int i=2; i<=sqr; i++) {
int temp =1;
int j;
for(j=i; j<=sqr; j++) {
temp*=j;
if(n%temp!=0)break;
}
if(j-i>len) {
len=j-i;
first = i;
}
}
if(len==0) printf("1\n%d", n);
else {
printf("%d\n",len);
for(int i=first; i<first+len; i++) {
if(i!=first)printf("*");
printf("%d", i);
}
}
return 0;
}
PAT Advanced 1096 Consecutive Factors (20) [数学问题-因子分解 逻辑题]的更多相关文章
- 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 (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,让你求最大的连续因子个数,并且输出其中最小的连续序列. 比如一个数可 ...
- 【PAT甲级】1096 Consecutive Factors (20 分)
题意: 输入一个int范围内的正整数,输出它最多可以被分解为多少个连续的因子并输出这些因子以*连接. trick: 测试点5包含N本身是一个素数的数据,此时应当输出1并把N输出. 测试点5包含一个2e ...
- PAT Advanced 1132 Cut Integer (20) [数学问题-简单数学]
题目 Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long ...
- PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
随机推荐
- 一百零一、SAP中ALV事件之十四,让ALV表格自动排序
如果我们需要对下图的凭证日期和物料进行排序,需要怎么做呢 一.我们来到ALV的定义 二.我们查看IT_SORT的定义,双击点进去 三.查看SLIS_T_SORTINFO_ALV定义 四.代码如下,定义 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring使用AspectJ开发AOP基于XML和基于Annotation
AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...
- CMD手动打jar包
代码: jar -cvfM "jarpage.jar" @fileslist.txt 解析: 将文档(fileslist.txt)中所有路径对应文件打成jar包,取名为:jarpa ...
- Windows2008R2安装DNS和SQLServer200r2服务 (9.18第七天)
原文网址:https://www.cnblogs.com/yankaohaitaiwei/p/11538205.html 二.IIS搭建web服务器 1.格式化D盘,一定要选择NTFS!!!不然后面添 ...
- div 100% 填充页面
css中 html,body{ margin:0; padding:0; height:100%; }
- C++ STD Gems03
transform.for_each #include <iostream> #include <vector> #include <string> #includ ...
- Java学习笔记(一) 面向对象---封装
面向对象---封装 封装是面向对象思想的三大特征之一. 理解: 隐藏对象的属性和实现细节,仅对外提供公共访问方式. 优点: 将变化隔离 便于使用 提升代码复用性 提高安全性 封装原则: 将不需要对外提 ...
- 十九、CI框架之数据库操作delete用法
一.代码如下: 二.执行f访问 三.查看数据库,已经id=15的数据已经被删掉了 不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微信二维码打赏任意金额给作者(微信号:382477247)哦, ...
- JS高级学习笔记(6)- 事件循环
参考文章:深入理解JS引擎的执行机制 JavaScript 异步.栈.事件循环.任务队列 我的笔记:ES系列之Promise async 和 await Event Loop 前提 js ...
- UVALive 6763 / CSU 1446
今天比赛的时候拿到的第一道题,其实挺简单的,求两等差序列中相同元素的个数,我想了一下就觉得,只要找到了第一个相等的点,然后后面求最大公约数就可以直接得到结果了 网上叫什么拓展欧几里得,我反正是按照我们 ...