本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91349859

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<).

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 ;
}

最新文章

  1. 【温故而知新-Javascript】使用地理定位
  2. MySQL执行存储过程权限
  3. 【ipython技巧】使用shell命令
  4. Delphi XE5 android listview
  5. android欢迎页源码
  6. centos下安装Jenkins轻松搞定
  7. paramiko 模块安装
  8. Python中从B类中调用A类的方法。
  9. toString()方法详解
  10. 详解基于MSSQL “order by”语句报错的SQL注入技术
  11. Socket看法
  12. 各个版本spring的jar包以及源码下载地址,目前最高版本到spring4.3.8,留存备用:
  13. linux 分区、目录及用途
  14. Python的time和datetime
  15. update-alternatives关键解疑
  16. 一些常用的排序算法(C版)
  17. Eclipse 导入 Android studio Exception Ljava/lang/UnsatisfiedLinkEror
  18. 图解PCB布线数字地、模拟地、电源地,单点接地抗干扰!
  19. SpringBoot 通过自定义注解实现AOP切面编程实例
  20. 【BZOJ】2142 礼物

热门文章

  1. 搭建JavaEE项目是遇到的几个问题
  2. 【leetcode刷题笔记】Remove Nth Node From End of List
  3. 小米5安装Xposed框架——需要解锁刷机
  4. Poj 1742 Coins(多重背包)
  5. 接口Comparator和Comparable的区别和联系
  6. Golang Channel用法简编
  7. 第 七 课 go的运算符
  8. 奇异值分解(SVD)详解
  9. python 基础 操作文件和目录
  10. 02_android下单元测试