PAT甲级——1096 Consecutive Factors (数学题)

本文同步发布在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最大的那组序列就是答案。

 1 #include <iostream>
 2 #include <vector>
 3 #include <cmath>
 4 using namespace std;
 5 int N, x;
 6 vector <int> ans, tmpAns;
 7 int main()
 8 {
 9     int i, product = 1;
10     scanf("%d", &N);
11     x = sqrt(N) + 1;
12     /*寻找最多的连续的数字,连续相乘的积能整除N*/
13     for (i = 2; i <= x; i++) {
14         product *= i;
15         tmpAns.push_back(i);
16         if (product <= N && N % product == 0 && tmpAns.size() > ans.size())//乘积小于N且能整除N又是目前找到的最多连续数字
17             ans = tmpAns;
18         else if(product > N || N % product != 0) {//乘积大于N或者新加入的i使得乘积无法整除N
19             tmpAns.clear();
20             if (N % i != 0)//新加入的i不能整除N,将product初始化
21                 product = 1;
22             else {//新加入的i是N的因子,意味着product的值超过了N,那么从前面开始删除节点,使得product小于N
23                 int tmpPro = product, j;
24                 for (j = 2; j <= i; j++) {
25                     tmpPro = tmpPro / j;
26                     if (tmpPro <= N && N % tmpPro == 0) {
27                         product = tmpPro;
28                         break;
29                     }
30                 }
31                 for (j++; j <= i; j++)
32                     tmpAns.push_back(j);
33             }
34         }
35     }
36     /*N为素数的时候输出它本身*/
37     if (ans.size() == 0)
38         printf("1\n%d\n", N);
39     else {
40         printf("%d\n", ans.size());
41         for (int i = 0; i < ans.size(); i++) {
42             if (i != 0)
43                 printf("*");
44             printf("%d", ans[i]);
45         }
46     }
47     return 0;
48 }

原文地址:https://www.cnblogs.com/yinhao-ing/p/10992481.html

时间: 2024-10-10 06:35:19

PAT甲级——1096 Consecutive Factors (数学题)的相关文章

PAT 甲级 1096 Consecutive Factors

https://pintia.cn/problem-sets/994805342720868352/problems/994805370650738688 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 con

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 maxim

1096. Consecutive Factors (20)——PAT (Advanced Level) Practise

题目信息 1096. Consecutive Factors (20) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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 consecu

PAT 1096. Consecutive Factors (20)

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

PAT 1096. Consecutive Factors

Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 356*7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum

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 maxim

PAT (Advanced Level) 1096. Consecutive Factors (20)

如果是素数直接输出1与素数,否则枚举长度和起始数即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; long long n; bool prime(

PAT甲题题解-1096. Consecutive Factors(20)-(枚举)

题意:一个正整数n可以分解成一系列因子的乘积,其中会存在连续的因子相乘,如630=3*5*6*7,5*6*7即为连续的因子.给定n,让你求最大的连续因子个数,并且输出其中最小的连续序列. 比如一个数可以分解2*3*4*6*7*8,最大的连续个数为3,因为存在两个,输出最小的那个即2*3*4. 首先,一个数如果是合数,那么它的因子必定不会超过sqrt(n)或者sqrt(n)+1.如果为质数,那么只可能为自己,因为题目说了不包括1. 我们先将2~sqrt(n)+1中为n的因子存到factor数组中,

1096 Consecutive Factors

// 因为N被连续整数 5*6*7整除的结果是3,而3只是相对于连续整数的产物,说明重点是找出连续整数,.//所以大致题意就是给出一个N,找出一段连续整数,使得N被整除. //观察样例给出的答案发现,N不会被除自身以外大于sqrt(N)的数整除.//可以用两个for循环暴力枚举. #include"iostream" #include"cmath" int main() { long long N; scanf("%lld",&N); l