算法提高 8-1因式分解
时间限制:10.0s 内存限制:256.0MB
问题描述
设计算法,用户输入合数,程序输出若个素数的乘积。例如,输入6,输出2*3。输入20,输出2*2*5。
样例
与上面的样例输入对应的输出。
例:
数据规模和约定
输入数据中每一个数在int表示范围内。
思路:注意不要加包名,会导致运行时错误
AC代码:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int x = 1;
boolean flag = true;
while(n != 1) {
x++;
while(n % x == 0) {
n /= x;
if(flag) {
flag = false;
System.out.print(x);
} else {
System.out.print("*" + x);
}
}
}
System.out.println();
}
}