Last updated
Was this helpful?
Last updated
Was this helpful?
package practice.algorithm.math;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Bj2609_1 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
if (a < b) {
int temp = a;
a = b;
b = temp;
}
System.out.println(gcd(a, b));
System.out.println(lcm(a, b));
}
private static int lcm(int a, int b) {
return a * b / gcd(a, b);
}
private static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
}
package practice.algorithm.math;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Bj2609_2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
if (a < b) {
int temp = a;
a = b;
b = temp;
}
System.out.println(gcd(a, b));
System.out.println(lcm(a, b));
}
private static int lcm(int a, int b) {
return a * b / gcd(a, b);
}
private static int gcd(int a, int b) {
while (b != 0) {
int r = a % b;
a = b;
b = r;
}
return a;
}
}a
수