1 package test_1_4; 2 3 import java.util.Scanner; 4 5 public class Factorization { 6 7 public static void main(String[] args) { 8 9 /** 将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5 */ 10 11 System.out.println("请输入一个数:"); 12 Scanner sc = new Scanner(System.in); 13 int num = sc.nextInt(); 14 15 factorize(num); 16 17 } 18 19 private static void factorize(int num) { 20 21 System.out.print(num + " = "); 22 int temp = num / 2; 23 for (int i = 2; i < temp; i++) { 24 while (num % i == 0) { 25 if (num == i) { 26 System.out.println(i); 27 } else { 28 System.out.print(i + " * "); 29 } 30 num /= i; 31 32 } 33 } 34 35 } 36 37 }
结果如下:
请输入一个数:
100
100 = 2 * 2 * 5 * 5
[20-04-26][Self-test 4]Java Factorization
原文:https://www.cnblogs.com/mirai3usi9/p/12781608.html