-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWA1017.java
36 lines (32 loc) · 919 Bytes
/
WA1017.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.ArrayList;
import java.util.Scanner;
public class WA1017 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num = input.nextInt();
System.out.print(num+"=");
ArrayList<Integer> numArr = primeFactors(num);
System.out.print(numArr.get(0));
for (int i = 1; i < numArr.size(); i++) {
System.out.print("*"+numArr.get(i));
}
}
public static ArrayList<Integer> primeFactors(int n){
ArrayList<Integer> numArr = new ArrayList<>();
while (n%2 == 0){
numArr.add(2);
n /=2;
}
for (int i = 3; i < Math.sqrt(n); i+=2 ) {
while (n%i == 0){
numArr.add(i);
n /= i;
}
}
//最后加上
if (n!=1){
numArr.add(n);
}
return numArr;
}
}