import javax.swing.JOptionPane;
public class findLCM {
public static void main(String[] args) {
String input1 = JOptionPane.showInputDialog(null,
"Enter the first number:", "input",
JOptionPane.QUESTION_MESSAGE);
int num1 = Integer.parseInt(input1);
String input2 = JOptionPane.showInputDialog(null,
"Enter the second number:", "input",
JOptionPane.QUESTION_MESSAGE);
int num2 = Integer.parseInt(input2);
double result = seekFactor(num1, num2);
JOptionPane.showMessageDialog(null, num1 + "," + num2 + "最大公约数为:"
+ result, "求最大公约数", JOptionPane.INFORMATION_MESSAGE);
}
static double seekFactor(int n1, int n2) {
int[][] answer1 = Factor(n1);
int[][] answer2 = Factor(n2);
printstring0(answer1);
printstring1(answer1);
double result = findResult(answer1, answer2);
return result;
}
static void printstring0(int[][] an) {
for (int i = 0; i < an.length; i++)
System.out.print(an[i][0]);
}
static void printstring1(int[][] an) {
for (int i = 0; i < an.length; i++)
System.out.println(an[i][1]);
}
static int[][] Factor(int n1) {
int i = -1, k = 2;
int[][] factor = new int[n1 / 2][2];
for (; k <= Math.sqrt(n1); k++ ) {
i++;
while (n1 % k == 0) {
n1 = n1 / k;
factor[i][0] = k;
factor[i][1] += 1;
}
}
return factor;
}
static double findResult(int[][] ans1, int[][] ans2) {
int i = 0, j = 0, k = 0;
double result = 0;
int[][] total = new int[60][2];
while (ans1[i][0] != 0 && ans2[j][0] != 0) {
if (ans1[i][0] == ans2[j][0]) {
if (ans1[i][1] >= ans2[j][1]) {
total[k][0] = ans1[i][0];
total[k][1] = ans1[i][1];
k++;
i++;
j++;
} else {
total[k][0] = ans1[i][0];
total[k][1] = ans2[j][1];
k++;
i++;
j++;
}
} else {
total[k][0] = ans1[i][0];
total[k][1] = ans1[i][1];
k++;
i++;
j++;
total[k][0] = ans2[j][0];
total[k][1] = ans2[j][1];
k++;
i++;
j++;
}
}
for (int m = 0; m < total.length; m++)
result = Math.pow(total[m][0], total[m][1]);
return result;
}
}
我做了好久都不能通过……希望有哪位好心人帮帮我……谢谢!!