题解 | #质数因子#
质数因子
https://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607
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 temp = in.nextLong(); StringBuffer returnString = new StringBuffer(""); long k = (long)Math.sqrt(temp); for(long i=2L;i <= k;i++){ if(temp % i == 0){ while(temp % i==0 && temp > 0){ returnString.append(i); returnString.append(" "); // 能除尽2 则一直除2 // 能除尽3 则一直除3 // 4 已经不存在 // 能除尽5 则一直除5 // ....... // 直到不能被除尽 则最后不能被除尽的值如果不是1 // 则它也是质因子 temp = temp/i; } } } if(temp != 1){ returnString.append(temp); } System.out.println(returnString); } }