Consecutive Factors 连续因素(Java语言)

简介: Consecutive Factors 连续因素(Java语言)

题目描述:

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<2

31

).

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 , where the factors are listed in increasing order, and 1 is NOT included.factor[1]factor[2]…*factor[k]


Sample Input:

630

Sample Output:

3
5*6*7

解题思路及代码:

双重循环,记录最长序列以及开始位置

注意:1不是因数,对于类似2的输入,一定要输出自己本身;


import java.util.Scanner;

public class ConsecutiveFactors连续因素 {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    int max=0,start=1;
    for(int i=2;i<=Math.sqrt(n);i++) {
      int s=i;
      int j=i+1;
      int temp = 0;
      while(n%s==0) {
          temp++;
          s=s*j;
          j++;
      }
      if(temp>max) {
        max=temp;
        start=i;
      }
    }
    if(max==0) {
      System.out.println("1");
      System.out.println(n);
    }
    else {
      System.out.println(max);
      for(int i=0,j=start;i<max;i++,j++)
        if(i==0) System.out.print(j);
        else System.out.print("*"+j);
    }
    
  }

}


相关文章
|
2月前
|
Java Maven
使用java语言制作一个窗体(弹窗),用来收集用户输入的内容
该博客文章介绍了如何使用Java Swing中的JFrame创建一个窗体来收集用户输入的内容,并提供了详细的实现步骤和完整代码示例。
使用java语言制作一个窗体(弹窗),用来收集用户输入的内容
|
3月前
|
Oracle 安全 Java
Java语言简介及发展
Java语言简介及发展
|
4月前
|
数据可视化 Java
Java语言使用DL4J实现图片分类
【6月更文挑战第14天】Java语言使用DL4J实现图片分类
85 3
|
3月前
|
算法 Java
Java语言实现最短路径算法(Shortest Path)
Java语言实现最短路径算法(Shortest Path)
45 3
|
2月前
|
Rust JavaScript Java
简单对比Java、Python、Go、Rust等常见语言计算斐波拉契数的性能
简单对比Java、Python、Go、Rust等常见语言计算斐波拉契数的性能
|
3月前
|
算法 Java 编译器
透视Java语言的究极优化:探索性能的深度
在Java程序员的日常工作中,优化代码性能是一项至关重要的任务。然而,除了传统的性能调优方法外,本文将探讨一些更为深奥的技术,如JIT编译器的内部工作机制、GC算法的进阶应用以及多线程并发模型的优化策略。通过深入了解这些技术背后的原理和实现,我们可以更好地理解如何在Java平台上实现最高效的代码运行。 【7月更文挑战第11天】
66 4
|
4月前
|
Java 容器
双指针(JAVA语言)
双指针(JAVA语言)
双指针(JAVA语言)
|
4月前
|
算法 Java
垃圾回收机制(Garbage Collection,GC)是Java语言的一个重要特性,它自动管理程序运行过程中不再使用的内存空间。
【6月更文挑战第24天】Java的GC自动回收不再使用的内存,关注堆中的对象。通过标记-清除、复制、压缩和分代等算法识别无用对象。GC分为Minor、Major和Full类型,针对年轻代、老年代或整个堆进行回收。性能优化涉及算法选择和参数调整。
54 3
|
4月前
|
Java 数据安全/隐私保护 开发者
Java是一种完全支持面向对象编程的语言,其面向对象特性包括封装、继承、多态和抽象等
【6月更文挑战第18天】**面向对象编程(OOP)通过对象封装状态和行为,实现问题域的抽象。Java全面支持OOP,核心特性包括**: - **封装**:保护数据安全,隐藏内部细节。 - **继承**:子类继承父类属性和行为,促进代码重用。 - **多态**:一个接口多种实现,增强灵活性和扩展性。 - **抽象**:通过接口和抽象类抽离共性,简化复杂性。 **Java的OOP便于理解和解决复杂系统问题。**
48 3
|
3月前
|
Java 大数据 API
Java语言的核心知识点与特性
Java 是一种广泛使用的编程语言,自 1995 年发布以来,它已经成为了企业级应用开发、移动应用开发、大数据处理和云计算等领域的主流技术。
40 0
下一篇
无影云桌面