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 ...
随机推荐
- Java 二叉树深度 判断平衡二叉树
package cookie; public class BTreeDepthIsBalanced { int depth(BNode head) { if (head == null) { retu ...
- Essay写作如何提升自己的辩驳水平?
辩证思维在英文写作上的表现方式有许多种,今天来讲讲Counterargument&Rebut,广泛用于英文写作和口语辩论.其作用就是通过辩驳和你论点相反的意见,来突出自己的论点更正确. 话说衡 ...
- P 1015 德才论
转跳点:
- hibernate注解 笔记
1.hibernate使用@where实现条件过滤功能 其里面只有一个参数clause,完整用法是: @Where(clause = "VALID_FLAG=1") 可以加在实体类 ...
- 用AD 输出BOM时提示failed to open excel template
解决办法: 去掉这个√!
- Session简单介绍
Session 会话 , Session是基于Cookie的一种会话机制. Cookie是服务器返回一小份数据给客户端,并且存放在客户端上. Session是,数据存放在服务器端. 常用API //得 ...
- <style scoped >中使用深度选择器影响子组件
摘自:https://blog.csdn.net/zhouzuoluo/article/details/95593143 <style scoped >中使用深度选择器影响子组件 在< ...
- JavaBean和json数据之间的转换(二)含有date类型的JavaBean
1.前言 上次讲了简单的JavaBean和json格式之间的转换,代码很简单,但是实际过程中,往往用到的JavaBean都是比较复杂的,其他的字段还好,如果JavaBean中包含了date类型的字段, ...
- JavaWeb之监听器
1. 介绍 监听器是一个专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动.监听器其实就是一个实现特定接口的普通java程 ...
- nginx如何一个域名多个端口?
方法一 写三个 listen server { listen 80; listen 81; listen 82; server_name www.sifou.com; ... 方法二 写三个serve ...