AcWing 1545. 质因子-java
原题链接
简单
作者:
Astarion
,
2021-03-08 15:51:28
,
所有人可见
,
阅读 370
import java.io.*;
class Main {
static InputStreamReader isr = new InputStreamReader(System.in);
static BufferedReader in = new BufferedReader(isr);
static OutputStreamWriter osw = new OutputStreamWriter(System.out);
static BufferedWriter out = new BufferedWriter(osw);
static void getPrimes(int n) throws IOException {
if (n == 1) {
out.write("1");
return;
}
for (int i = 2; i <= n / i; i++) {
boolean flag = false;
if (n % i == 0) {
flag = true;
int cnt = 0;
while (n % i == 0) {
cnt++;
n /= i;
}
if (cnt == 1) {
out.write(i+"");
} else {
out.write(i+"^"+cnt);
}
}
if (n > 1 && flag) {
out.write("*");
}
}
if (n > 1) {
out.write(n+"");
}
}
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(in.readLine());
out.write(n+"=");
getPrimes(n);
in.close();
isr.close();
out.flush();
out.close();
osw.close();
}
}