HDU1164 Eddy's research I (试除法)
程序员文章站
2022-07-05 16:59:49
...
试除法
根据题意处理一些边边角角的问题即可提交。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int N = 20, k;
static int prime[];
public static void main(String[] args) throws IOException {
while (true) {
String s = br.readLine();
if (s == null) break;
int x = Integer.parseInt(s);
divide(x);
for (int i = 0; i < k - 1; i++) System.out.print(prime[i] + "*");
System.out.println(prime[k - 1]);
}
}
public static void divide(int x) {
prime = new int[N];
k = 0;
for (int i = 2; i <= x / i; i++) {
if (x % i == 0) {
prime[k++] = i;
x /= i;
i--;
}
}
prime[k++] = x;
Arrays.sort(prime, 0, k);
}
}