解题思路:

质数是从2开始的,然后求一个数的约数最大数是这个数的平方根,因为一旦这个数可以开平方,则不是质数,也就可以被整除,但是如果从2开始到用户输入的这个数结束则会出现超时的情况。

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        long a = 0; 
        while (in.hasNextLong()) { // 注意 while 处理多个 case
            a = in.nextLong();
            for(int i=2;i<=Math.sqrt(a);++i){
                while(a%i==0){
                    a/=i;
                    System.out.print(i+" ");
                    break;
                }
            }
            System.out.println(a==1?"":a+"");
        }
    }
}

优化后的代码,主要优化点为通过流来读取用户输入的信息。

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader sb = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
        while ((str = sb.readLine()) != null) {
            long l = Long.parseLong(str);
            for (int i = 2; i <= (long) Math.sqrt(l); i++) {
                while (l % i == 0) {
                    System.out.print(i + " ");
                    l /= i;
                }
            }
            if (l != 1)
                System.out.println(l);
        }
        sb.close();
    }
}