求最小公倍数/最小公约数(Java)
求最小公倍数
http://www.nowcoder.com/questionTerminal/22948c2cad484e0291350abad86136c3
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner s = new Scanner(System.in); int n1 = s.nextInt(); int n2 = s.nextInt(); method2(n1,n2); } private static void method1(int n1, int n2){ if (n1 > n2){ int temp = n1; n1 = n2; n2 = temp; } for (int i = 1; i <= n1; i++){ if ((n2 * i) % n1 == 0){ System.out.println(n2*i); break; } } } private static void method2(int n1, int n2){ int n3 = gcd(n1, n2); System.out.println(n1*n2/n3); } private static int gcd(int n1, int n2){ if (n2 == 0) return n1; return gcd(n2, n1%n2); } }