PAT甲级——1096 Consecutive Factors (数学题)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91349859
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<).
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
5*6*7
题目大意:一个数字可以写成若干因子相乘的形式,在这些因子中寻找连续的数字形成一个序列,使得此序列的成员个数最多,输出成员个数最多且数值最小的序列,写成相乘的格式。
思路:直接从2遍历到sqrt( N ) +1,找能整除N的连续乘积,此乘积的连续数字存入tmpAns数组中,size最大的那组序列就是答案。
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int N, x;
vector <int> ans, tmpAns;
int main()
{
int i, product = ;
scanf("%d", &N);
x = sqrt(N) + ;
/*寻找最多的连续的数字,连续相乘的积能整除N*/
for (i = ; i <= x; i++) {
product *= i;
tmpAns.push_back(i);
if (product <= N && N % product == && tmpAns.size() > ans.size())//乘积小于N且能整除N又是目前找到的最多连续数字
ans = tmpAns;
else if(product > N || N % product != ) {//乘积大于N或者新加入的i使得乘积无法整除N
tmpAns.clear();
if (N % i != )//新加入的i不能整除N,将product初始化
product = ;
else {//新加入的i是N的因子,意味着product的值超过了N,那么从前面开始删除节点,使得product小于N
int tmpPro = product, j;
for (j = ; j <= i; j++) {
tmpPro = tmpPro / j;
if (tmpPro <= N && N % tmpPro == ) {
product = tmpPro;
break;
}
}
for (j++; j <= i; j++)
tmpAns.push_back(j);
}
}
}
/*N为素数的时候输出它本身*/
if (ans.size() == )
printf("1\n%d\n", N);
else {
printf("%d\n", ans.size());
for (int i = ; i < ans.size(); i++) {
if (i != )
printf("*");
printf("%d", ans[i]);
}
}
return ;
}
PAT甲级——1096 Consecutive Factors (数学题)的更多相关文章
- PAT 甲级 1096 Consecutive Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805370650738688 Among all the factors ...
- PAT甲级——A1096 Consecutive Factors【20】
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 1096 Consecutive Factors[难]
1096 Consecutive Factors (20 分) Among all the factors of a positive integer N, there may exist sever ...
- 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 ...
- PAT 1096. Consecutive Factors
Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...
- 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 (20 分)
题意: 输入一个int范围内的正整数,输出它最多可以被分解为多少个连续的因子并输出这些因子以*连接. trick: 测试点5包含N本身是一个素数的数据,此时应当输出1并把N输出. 测试点5包含一个2e ...
- PAT (Advanced Level) 1096. Consecutive Factors (20)
如果是素数直接输出1与素数,否则枚举长度和起始数即可. #include<cstdio> #include<cstring> #include<cmath> #in ...
随机推荐
- STL defalloc.h
defalloc.h . // Filename: defalloc.h . . // Comment By: 凝霜 . // E-mail: mdl2009@vip.qq.com . // Blog ...
- Java中的泛型、枚举和注解
1.泛型: 一.为什么要有泛型(Generic)?1.解决元素存储的安全性问题任何类型都可以添加到集合中:类型不安全2.解决获取数据元素时,需要类型强转的问题读取出来的对象需要强转:繁琐可能有Clas ...
- 【IPC通信】key_t键和ftok函数
System V IPC分为三种: System V消息队列 System V信号量 System V共享内存区 这三种类型的IPC使用key_t值做为它们的名字. key_t这个数据类型在<s ...
- redis的五种数据类型及应用场景
前言 redis是用键值对的形式来保存数据,键类型只能是String,但是值类型可以有String.List.Hash.Set.Sorted Set五种,来满足不同场景的特定需求. 本博客中的示例不是 ...
- css 更换浏览器 默认图标
cursor:url("./images/test.cur"),auto; 只在chrome测试过...
- python文件操作 seek(),tell()
seek():移动文件读取指针到指定位置 tell():返回文件读取指针的位置 seek()的三种模式: (1)f.seek(p,0) 移动当文件第p个字节处,绝对位置 (2)f.seek(p,1) ...
- Netty组件
一.Channel.EventLoop 和ChannelFuture 这些类合在一起,可以被认为是Netty 网络抽象的代表: Channel—Socket: EventLoop—控制流.多线程处理. ...
- Elasticsearch2.x --DeleteByQuery
一.安装插件 要删除某个索引的一个type下的所有文档,相当于关系型数据库中的清空表操作.查阅了一些资料可以通过Delete-by-Query插件删除,首先使用插件管理器安装Delete-by-Que ...
- codeforces educational round25
A #include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ; string s; c ...
- [dp]编辑距离问题
https://www.51nod.com/tutorial/course.html#!courseId=3 转移方程: 注意如何对齐的. 这个算法的特点是,S和T字符串左边始终是对齐的.为了更好地理 ...