题目:将一个正整数分解质因数。例如:输入 90,打印出
90=2*3*3*5
。质因数,都是质数,判断是不是质数是的话直接输出,不是再分解
package myself;
import java.util.Scanner;
/**
* @Auther QY
* @Date 2023/12/11
*/
public class four {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
isPrime(n);
}
public static void isPrime(int n) {
boolean flag = true;
for (int i = 2; i * i < n; i++) {
if (n % i == 0) {
flag = false;
}
}
if (flag) {
System.out.println("无法分解,是合数");
} else {
play(n);
}
}
public static void play(int n) {
int temp = n;
String res = "" + n + "=";
int k = 2;
int multiplication = 1;
while (multiplication != n) {
for (int i = k; i <= n; i++) {
if (temp % i == 0) {
res += i;
multiplication *= i;
temp /= i;
if (multiplication != n) {
res += "*";
}
break;
}
}
}
System.out.println(res);
}
}
这题唯一麻烦的感觉就是输出格式,仔细点