[PAT] 1096 Consecutive Factors (20 分)Java

本文介绍了一个算法问题,即寻找正整数N的最大连续因子序列,并提供了一段Java代码实现。问题要求找出N的所有因子中,最长的连续数字序列,并打印出序列的长度及具体数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×\times×5×\times×6×\times×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<2^{31}<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
5*6*7
作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
 
 
 
 1 package pattest;
 2 
 3 import java.util.Scanner;
 4 
 5 /**
 6  * @Auther: Xingzheng Wang
 7  * @Date: 2019/2/27 20:05
 8  * @Description: pattest
 9  * @Version: 1.0
10  */
11 public class PAT1096 {
12     public static void main(String[] args) {
13         Scanner sc = new Scanner(System.in);
14         int num = sc.nextInt();
15         int temp, first = 0, len = 0;
16         double maxn = Math.sqrt(Double.valueOf(num));
17         for (int i = 2; i <= maxn; i++) {
18             int j;
19             temp = 1;
20             for (j = i; j <= maxn; j++) {
21                 temp *= j;
22                 if (num % temp != 0) break;
23             }
24             if (j - i > len) {
25                 len = j - i;
26                 first = i;
27             }
28         }
29         if (first == 0) {
30             System.out.println(1);
31             System.out.print(num);
32         } else {
33             System.out.println(len);
34             for (int i = 0; i < len; i++) {
35                 System.out.print(first + i);
36                 if (i != len - 1) System.out.print("*");
37             }
38         }
39     }
40 }

 

转载于:https://www.cnblogs.com/PureJava/p/10498236.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值