A1096 Consecutive Factors (20 point(s))

连续因子的计算630-> 5*6*7

1. 原文

原题

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:

1
630

Sample output:

1
2
3
5*6*7

2. 解析

不存在连续因子的判断

1
2
3
4
if(first==0){
printf("1\n%d",n);
return 0;
}

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<cstdio>
#include<cmath>
int main()
{
int n;
scanf("%d",&n);
int sqr=sqrt(1.0*n);
int cnt=0;
int first=0;
for (int i = 2; i <= sqr; ++i)
{
int j=i;
int temp=1;
for (; j <= sqr; ++j)
{
temp*=j;
if (n%temp!=0)
{
break;
}
}
if (j-i>cnt)
{
cnt=j-i;
first=i;
}
}
if(first==0){
printf("1\n%d",n);
return 0;

}
printf("%d\n",cnt);
for (int i = 0; i < cnt; ++i)
{
if (i!=0)
{
printf("*");
}
printf("%d",first+i);
}
printf("\n");
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1096/
  • 发布时间: 2019-09-03 12:31
  • 更新时间: 2021-10-29 14:07
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!