Java最大公约数和最小公倍数的求法(辗转相除法)
生活随笔
收集整理的這篇文章主要介紹了
Java最大公约数和最小公倍数的求法(辗转相除法)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這道題計算了三個數的最小公倍數
1 import java.util.Scanner; 2 3 public class D { 4 public static int gcd(int a,int b) { 5 int max = a>b?a:b; 6 int min = a<b?a:b; 7 if(max%min != 0) { 8 return gcd(min,max%min); 9 } 10 else 11 return min; 12 } 13 public static int lcm(int a,int b) { 14 return a*b/gcd(a,b); 15 } 16 public static void main(String[] args) { 17 // TODO Auto-generated method stub 18 int a,b,c; 19 Scanner in = new Scanner(System.in); 20 a = in.nextInt(); 21 b = in.nextInt(); 22 c = in.nextInt(); 23 int d = lcm(a,b); 24 d = lcm(d,c); 25 System.out.println(d); 26 return; 27 }?
轉載于:https://www.cnblogs.com/xuyanqd/p/8590544.html
總結
以上是生活随笔為你收集整理的Java最大公约数和最小公倍数的求法(辗转相除法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果手机耳机设置(苹果手机耳机设置在哪)
- 下一篇: Javac可以编译,Java显示找不到或