将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
1 import java.util.Scanner; 2 3 public class Algorithm_Game_04 { 4 public static void main(String[] args) { 5 6 Scanner scanner = new Scanner(System.in); 7 8 int n = scanner.nextInt(); 9 String str = n + " = 1*"; 10 int max = 1 ; 11 for(int i = 2 ; i <= n ; i++){ 12 if(n%i==0){ 13 max*=i; 14 n=n/i; 15 str +=(i+"*"); 16 i--; 17 } 18 if(max==n)break; 19 } 20 if(max==1){ 21 str+=n; 22 } 23 System.out.println(str.substring(0,str.length()-1)); 24 } 25 }